-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (82 loc) · 2.7 KB
/
main.go
File metadata and controls
94 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright © 2026 Benny Powers <web@bennypowers.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Command mappa generates and works with ES module import maps.
package main
import (
"errors"
"fmt"
"os"
"runtime/pprof"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"bennypowers.dev/mappa/cmd/generate"
"bennypowers.dev/mappa/cmd/inject"
"bennypowers.dev/mappa/cmd/trace"
"bennypowers.dev/mappa/cmd/validate"
"bennypowers.dev/mappa/cmd/version"
)
var (
cpuprofile string
cpuprofileFile *os.File
rootCmd = &cobra.Command{
Use: "mappa",
Short: "Generate and work with ES module import maps",
Long: `mappa generates ES module import maps from package.json dependencies.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
return fmt.Errorf("could not create CPU profile: %w", err)
}
cpuprofileFile = f
if err := pprof.StartCPUProfile(f); err != nil {
closeErr := f.Close()
return errors.Join(
fmt.Errorf("could not start CPU profile: %w", err),
closeErr,
)
}
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if cpuprofileFile != nil {
pprof.StopCPUProfile()
if err := cpuprofileFile.Close(); err != nil {
return fmt.Errorf("closing CPU profile: %w", err)
}
}
return nil
},
}
)
func init() {
// Root flags (persistent across all commands)
rootCmd.PersistentFlags().StringP("package", "p", ".", "Package directory")
rootCmd.PersistentFlags().StringP("output", "o", "", "Output file (default: stdout)")
rootCmd.PersistentFlags().StringVar(&cpuprofile, "cpuprofile", "", "Write CPU profile to file")
_ = viper.BindPFlag("package", rootCmd.PersistentFlags().Lookup("package"))
_ = viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output"))
// Add commands (alphabetized)
rootCmd.AddCommand(generate.Cmd)
rootCmd.AddCommand(inject.Cmd)
rootCmd.AddCommand(trace.Cmd)
rootCmd.AddCommand(validate.Cmd)
rootCmd.AddCommand(version.Cmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}