github.com/derat/nup@v0.0.0-20230418113745-15592ba7c620/cmd/nup/main.go (about) 1 // Copyright 2021 Daniel Erat. 2 // All rights reserved. 3 4 package main 5 6 import ( 7 "context" 8 "flag" 9 "fmt" 10 "os" 11 "path/filepath" 12 13 "github.com/derat/nup/cmd/nup/check" 14 "github.com/derat/nup/cmd/nup/client" 15 "github.com/derat/nup/cmd/nup/config" 16 "github.com/derat/nup/cmd/nup/covers" 17 "github.com/derat/nup/cmd/nup/debug" 18 "github.com/derat/nup/cmd/nup/dump" 19 "github.com/derat/nup/cmd/nup/metadata" 20 "github.com/derat/nup/cmd/nup/query" 21 "github.com/derat/nup/cmd/nup/storage" 22 "github.com/derat/nup/cmd/nup/update" 23 "github.com/google/subcommands" 24 ) 25 26 func main() { 27 // The subcommands package generates the usage string. 28 configFile := flag.String("config", filepath.Join(os.Getenv("HOME"), ".nup/config.json"), 29 "Path to config file") 30 31 subcommands.Register(subcommands.CommandsCommand(), "") 32 subcommands.Register(subcommands.FlagsCommand(), "") 33 subcommands.Register(subcommands.HelpCommand(), "") 34 35 var cfg client.Config 36 subcommands.Register(&check.Command{Cfg: &cfg}, "") 37 subcommands.Register(&config.Command{Cfg: &cfg}, "") 38 subcommands.Register(&covers.Command{Cfg: &cfg}, "") 39 subcommands.Register(&debug.Command{Cfg: &cfg}, "") 40 subcommands.Register(&dump.Command{Cfg: &cfg}, "") 41 subcommands.Register(&metadata.Command{Cfg: &cfg}, "") 42 subcommands.Register(&projectidCommand{cfg: &cfg}, "") 43 subcommands.Register(&query.Command{Cfg: &cfg}, "") 44 subcommands.Register(&storage.Command{Cfg: &cfg}, "") 45 subcommands.Register(&update.Command{Cfg: &cfg}, "") 46 47 flag.Parse() 48 49 if cmd := flag.Arg(0); cmd != "commands" && cmd != "flags" && cmd != "help" { 50 if err := client.LoadConfig(*configFile, &cfg); err != nil { 51 fmt.Fprintln(os.Stderr, "Unable to read config file:", err) 52 os.Exit(int(subcommands.ExitUsageError)) 53 } 54 } 55 56 os.Exit(int(subcommands.Execute(context.Background()))) 57 } 58 59 // This is too simple to get its own package. 60 type projectidCommand struct{ cfg *client.Config } 61 62 func (*projectidCommand) Name() string { return "projectid" } 63 func (*projectidCommand) Synopsis() string { return "print GCP project ID" } 64 func (*projectidCommand) Usage() string { 65 return `projectid: 66 Print the Google Cloud Platform project ID (as derived from the 67 config's serverURL field). 68 69 ` 70 } 71 func (cmd *projectidCommand) SetFlags(f *flag.FlagSet) {} 72 func (cmd *projectidCommand) Execute(ctx context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { 73 pid, err := cmd.cfg.ProjectID() 74 if err != nil { 75 fmt.Fprintln(os.Stderr, "Failed getting project ID:", err) 76 return subcommands.ExitFailure 77 } 78 fmt.Println(pid) 79 return subcommands.ExitSuccess 80 }