Last active
December 17, 2020 00:00
-
-
Save kprav33n/835643597cbf689a8ef269602344f0ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path" | |
"path/filepath" | |
"github.com/spf13/cobra" | |
) | |
type config struct { | |
buildFile string | |
name string | |
level int | |
} | |
func preRun(cmd *cobra.Command, args []string, cfg *config) error { | |
buildFile, _ := cmd.Flags().GetString("build-file") | |
fmt.Printf("Read build-file %s and populate config here\n", buildFile) | |
cfg.buildFile = buildFile | |
cfg.name = "bob" | |
cfg.level = 3 | |
return nil | |
} | |
func buildCmd(cfg *config) *cobra.Command { | |
cmd := &cobra.Command{ // nolint:exhaustivestruct | |
Use: "build", | |
Short: "builds service binaries", | |
Long: "", | |
Run: func(cmd *cobra.Command, args []string) { | |
runBuild(cmd, args, cfg) | |
}, | |
} | |
return cmd | |
} | |
func runBuild(cmd *cobra.Command, args []string, cfg *config) { | |
fmt.Printf("do build: %+v\n", cfg) | |
} | |
func main() { | |
name := filepath.Base(os.Args[0]) | |
var cfg config | |
rootCmd := &cobra.Command{ // nolint:exhaustivestruct | |
Use: name, | |
Short: "builder of blobs", | |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | |
return preRun(cmd, args, &cfg) | |
}, | |
} | |
rootCmd.PersistentFlags().StringP("build-file", "f", path.Join( | |
func() string { | |
s, _ := os.Getwd() | |
return s | |
}(), "bob.yaml"), | |
"build file (default: $PWD/bob.yaml)") | |
// Add all the sub-commands | |
rootCmd.AddCommand(buildCmd(&cfg)) | |
if err := rootCmd.Execute(); err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment