-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Cobra for defining CLI commands and flags (#282)
* Using Cobra for defining CLI commands and flags Also, making Provider a proper type Signed-off-by: Ziv Nevo <nevo@il.ibm.com>
- Loading branch information
Showing
13 changed files
with
191 additions
and
128 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2023- IBM Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/np-guard/cloud-resource-collector/pkg/common" | ||
"github.com/np-guard/cloud-resource-collector/pkg/factory" | ||
) | ||
|
||
func collectResources(cmd *cobra.Command, _ []string) error { | ||
cmd.SilenceUsage = true // if we got this far, flags are syntactically correct, so no need to print usage | ||
|
||
if provider != common.IBM { | ||
if len(regions) > 0 { | ||
return fmt.Errorf("setting regions from the command-line for provider %s is not yet supported. "+ | ||
"Use environment variables or config files instead", provider) | ||
} | ||
if resourceGroupID != "" { | ||
return fmt.Errorf("setting resource-group from the command-line for provider %s is not yet supported. ", provider) | ||
} | ||
} | ||
|
||
resources := factory.GetResourceContainer(provider, regions, resourceGroupID) | ||
// Collect resources from the provider API and generate output | ||
err := resources.CollectResourcesFromAPI() | ||
if err != nil { | ||
return err | ||
} | ||
OutputResources(resources, outputFile) | ||
resources.PrintStats() | ||
return nil | ||
} |
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright 2023- IBM Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/np-guard/cloud-resource-collector/pkg/common" | ||
"github.com/np-guard/cloud-resource-collector/pkg/factory" | ||
"github.com/np-guard/cloud-resource-collector/pkg/version" | ||
) | ||
|
||
const ( | ||
providerFlag = "provider" | ||
) | ||
|
||
var ( | ||
provider common.Provider | ||
regions []string | ||
resourceGroupID string | ||
outputFile string | ||
) | ||
|
||
func newRootCommand() *cobra.Command { | ||
rootCmd := &cobra.Command{ | ||
Use: "collector", | ||
Short: "cloud-resource-collector is a CLI for collecting VPC-related cloud resources", | ||
Long: `cloud-resource-collector uses cloud-provider SDK to gather VPC-related resources defining network connectivity`, | ||
Version: version.VersionCore, | ||
} | ||
|
||
rootCmd.PersistentFlags().VarP(&provider, providerFlag, "p", "collect resources from an account in this cloud provider") | ||
_ = rootCmd.MarkPersistentFlagRequired(providerFlag) | ||
|
||
rootCmd.AddCommand(newCollectCommand()) | ||
rootCmd.AddCommand(newGetRegionsCommand()) | ||
|
||
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) // disable help command. should use --help flag instead | ||
|
||
return rootCmd | ||
} | ||
|
||
func newCollectCommand() *cobra.Command { | ||
collectCmd := &cobra.Command{ | ||
Use: "collect", | ||
Short: "Collect VPC-related cloud resources", | ||
Long: `Use cloud-provider SDK to gather VPC-related resources defining network connectivity`, | ||
Args: cobra.NoArgs, | ||
RunE: collectResources, | ||
} | ||
|
||
collectCmd.Flags().StringArrayVarP(®ions, "region", "r", nil, "cloud region from which to collect resources") | ||
collectCmd.Flags().StringVar(&resourceGroupID, "resource-group", "", "resource group id or name from which to collect resources") | ||
collectCmd.Flags().StringVar(&outputFile, "out", "", "file path to store results") | ||
|
||
return collectCmd | ||
} | ||
|
||
func newGetRegionsCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "get-regions", | ||
Short: "List available regions for a given provider", | ||
Long: `List all regions that can be used with the --region flag`, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
if provider != common.IBM { | ||
return fmt.Errorf("command not supported for provider %s", provider) | ||
} | ||
resources := factory.GetResourceContainer(provider, nil, "") | ||
providerRegions := strings.Join(resources.AllRegions(), ", ") | ||
fmt.Printf("Available regions for provider %s: %s\n", provider, providerRegions) | ||
return nil | ||
}, | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright 2023- IBM Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
type Provider string | ||
|
||
const ( | ||
AWS Provider = "aws" | ||
IBM Provider = "ibm" | ||
) | ||
|
||
var AllProviders = []string{string(IBM), string(AWS)} | ||
|
||
func (p *Provider) String() string { | ||
return string(*p) | ||
} | ||
|
||
func (p *Provider) Set(v string) error { | ||
v = strings.ToLower(v) | ||
if slices.Contains(AllProviders, v) { | ||
*p = Provider(v) | ||
return nil | ||
} | ||
return fmt.Errorf("must be one of [%s]", strings.Join(AllProviders, ", ")) | ||
} | ||
|
||
func (p *Provider) Type() string { | ||
return "string" | ||
} |
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
Oops, something went wrong.