11package cli
22
33import (
4- "errors"
54 "flag"
65 "fmt"
76 "net/url"
@@ -15,7 +14,7 @@ import (
1514 "github.com/peterbourgon/ff/v3"
1615)
1716
18- func validateFlags (flags * types.CliFlags ) error {
17+ func ValidateFlags (flags * types.CliFlags ) error {
1918 if ! utils .IsSupportedPlatformArch (flags .Platform , flags .Architecture ) {
2019 return fmt .Errorf (
2120 "invalid combination of platform+architecture detected: %s+%s" ,
@@ -30,8 +29,6 @@ func validateFlags(flags *types.CliFlags) error {
3029 }
3130 if manifestURL .Scheme == "https" {
3231 flags .ManifestURL = true
33- } else if len (flags .ExecCommand ) == 0 {
34- return errors .New ("invalid path/url to manifest file" )
3532 }
3633 }
3734 if info , err := os .Stat (flags .DownloadPath ); ! (err == nil && info .IsDir ()) {
@@ -47,38 +44,12 @@ func validateFlags(flags *types.CliFlags) error {
4744// with useful values set after parsing the same.
4845func GetCliFlags (buildFlags types.BuildFlags ) (types.CliFlags , error ) {
4946 cliFlags := types.CliFlags {}
50- args := []string {}
51- // Handle post-exec string
52- for i , arg := range os .Args [1 :] {
53- if arg == "--" {
54- cliFlags .ExecCommand = os .Args [i + 2 :]
55- break
56- }
57- args = append (args , arg )
58- }
47+
5948 flag .Set ("logtostderr" , "true" )
6049 vFlag := flag .Lookup ("v" )
6150 fs := flag .NewFlagSet (constants .AppName , flag .ExitOnError )
6251
63- goos := runtime .GOOS
64- if os .Getenv ("GOOS" ) != "" {
65- goos = os .Getenv ("GOOS" )
66- }
67-
68- goarch := runtime .GOARCH
69- if os .Getenv ("GOARCH" ) != "" {
70- goarch = os .Getenv ("GOARCH" )
71- }
72-
73- fs .StringVar (& cliFlags .Verbosity , "v" , "3" , "Log verbosity. Integer value from 0 to 9" )
74- fs .StringVar (& cliFlags .Platform , "platform" , goos , "One of linux/windows/darwin" )
75- fs .StringVar (& cliFlags .Architecture , "architecture" , goarch , "System architecture (amd64/arm64)" )
76- fs .StringVar (& cliFlags .DownloadPath , "path" , fmt .Sprintf (".%sbin" , string (os .PathSeparator )), "Path to store binaries" )
77- fs .StringVar (& cliFlags .ManifestFile , "manifest" , "manifest.yaml" , "Path (or URL) to manifest yaml file" )
78- fs .BoolVar (& cliFlags .SkipDownloaded , "skip-downloaded" , false , "Skip already downloaded archive (if found)" )
79- fs .BoolVar (& cliFlags .Cleanup , "cleanup" , true , "Cleanup downloaded archives after extraction" )
80- fs .BoolVar (& cliFlags .UpdateManifest , "update-manifest" , false , "Update the manifest file commit shas from releases prior to downloading" )
81- fs .BoolVar (& cliFlags .Download , "download" , true , "Actually do a download. Only useful for -update-manifest=true -download=false" )
52+ AddDownloaderFlags (fs , & cliFlags )
8253
8354 version := fs .Bool ("version" , false , "Get version information" )
8455
@@ -88,18 +59,40 @@ func GetCliFlags(buildFlags types.BuildFlags) (types.CliFlags, error) {
8859 }
8960
9061 ff .Parse (
91- fs , args ,
92- ff .WithConfigFileFlag ("config" ),
62+ fs , os .Args [1 :],
9363 ff .WithConfigFileParser (ff .PlainParser ),
9464 ff .WithEnvVarPrefix ("CATALYST_DOWNLOADER" ),
9565 ff .WithEnvVarSplit ("," ),
9666 )
9767 flag .CommandLine .Parse (nil )
9868 vFlag .Value .Set (cliFlags .Verbosity )
9969
100- err := validateFlags (& cliFlags )
70+ err := ValidateFlags (& cliFlags )
10171 if err != nil {
10272 glog .Fatal (err )
10373 }
10474 return cliFlags , err
10575}
76+
77+ // Populate a provided flagset with downloader flags
78+ func AddDownloaderFlags (fs * flag.FlagSet , cliFlags * types.CliFlags ) {
79+ goos := runtime .GOOS
80+ if os .Getenv ("GOOS" ) != "" {
81+ goos = os .Getenv ("GOOS" )
82+ }
83+
84+ goarch := runtime .GOARCH
85+ if os .Getenv ("GOARCH" ) != "" {
86+ goarch = os .Getenv ("GOARCH" )
87+ }
88+
89+ fs .StringVar (& cliFlags .Verbosity , "v" , "3" , "Log verbosity. Integer value from 0 to 9" )
90+ fs .StringVar (& cliFlags .Platform , "platform" , goos , "One of linux/windows/darwin" )
91+ fs .StringVar (& cliFlags .Architecture , "architecture" , goarch , "System architecture (amd64/arm64)" )
92+ fs .StringVar (& cliFlags .DownloadPath , "path" , fmt .Sprintf (".%sbin" , string (os .PathSeparator )), "Path to store binaries" )
93+ fs .StringVar (& cliFlags .ManifestFile , "manifest" , "manifest.yaml" , "Path (or URL) to manifest yaml file" )
94+ fs .BoolVar (& cliFlags .SkipDownloaded , "skip-downloaded" , false , "Skip already downloaded archive (if found)" )
95+ fs .BoolVar (& cliFlags .Cleanup , "cleanup" , true , "Cleanup downloaded archives after extraction" )
96+ fs .BoolVar (& cliFlags .UpdateManifest , "update-manifest" , false , "Update the manifest file commit shas from releases prior to downloading" )
97+ fs .BoolVar (& cliFlags .Download , "download" , true , "Actually do a download. Only useful for -update-manifest=true -download=false" )
98+ }
0 commit comments