Skip to content

Commit

Permalink
feat: add a possibility to configure pass through a configuration file (
Browse files Browse the repository at this point in the history
#58)

Co-authored-by: David MICHENEAU <[email protected]>
  • Loading branch information
dmicheneau and David MICHENEAU authored Jan 19, 2024
1 parent 15d7d5c commit 54f8c81
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 128 deletions.
3 changes: 3 additions & 0 deletions .changelog/57.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feat
config: Add a config file configuration.
```
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you submit change in the provider code, please make sure to:

### How has this code been coverage and tested
```
go test -v -coverprofile=coverage.out ./cmd/... && go tool cover -func=coverage.out
go test -coverprofile=coverage.out ./cmd/... && go tool cover -func=coverage.out
.... <PUSH YOUR RESULT HERE>
```

Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lintWithFix:

install:
golangci-lint run
go build -o cav .
go build -o /usr/local/bin/cav .

run:
go run .
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
:warning: This tool is under development, Big improvements or changes may appear.

# cloudavenue-cli
# Whatis cav
`cav` is a CLI terminal app built to give a basic view to manage your Cloud Avenue IaaS.

# Install

Please Go in Getting Start: https://github.com/orange-cloudavenue/cloudavenue-cli/blob/main/docs/index.md
Please see Install chapter in Getting Start: https://github.com/orange-cloudavenue/cloudavenue-cli/blob/main/docs/index.md#Installation

# usage
# Configuration

Please see Configuration chapter in Getting Start: https://github.com/orange-cloudavenue/cloudavenue-cli/blob/main/docs/index.md#Configuration

# Usage
* Navigation commands :

```shell
Expand Down
18 changes: 13 additions & 5 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {
var createPublicIPCmd = &cobra.Command{
Use: "publicip",
Short: "Create an ip",
Example: "ip create --name <>",
Example: "ip create --name <EdgeGateway>",

Run: func(cmd *cobra.Command, args []string) {
// Check if time flag is set
Expand All @@ -87,8 +87,10 @@ var createPublicIPCmd = &cobra.Command{
}

// Create a public ip
s.Stop()
fmt.Println("create public ip resource")
fmt.Println("for EdgeGateway name: " + gwName)
s.Restart()

job, err := c.V1.PublicIP.New(gwName)
if err != nil {
Expand All @@ -100,8 +102,8 @@ var createPublicIPCmd = &cobra.Command{
fmt.Println("Error during public ip creation !!", err)
return
}
s.FinalMSG = "public ip resource created successfully !!"
s.Stop()
fmt.Println("public ip resource created successfully !")
},
}

Expand All @@ -125,8 +127,10 @@ var createVDCCmd = &cobra.Command{
}

// Create the vdc
s.Stop()
fmt.Println("create vdc resource (with basic value)")
fmt.Println("vdc name: " + vdcName)
s.Restart()

if _, err = c.V1.VDC().New(&infrapi.CAVVirtualDataCenter{
VDC: infrapi.CAVVirtualDataCenterVDC{
Expand All @@ -151,8 +155,8 @@ var createVDCCmd = &cobra.Command{
fmt.Println("Error from vdc", err)
return
}
s.FinalMSG = "vdc resource created successfully !!\n"
s.Stop()
fmt.Println("vdc resource created successfully !")
},
}

Expand Down Expand Up @@ -194,9 +198,11 @@ var createEdgeGatewayCmd = &cobra.Command{
}
}
// Create the edgeGateway
s.Stop()
fmt.Println("Creating EdgeGateway resource")
fmt.Println("vdc name: " + vdc)
fmt.Println("t0 name: " + t0)
s.Restart()
job, err := c.V1.EdgeGateway.New(vdc, t0)
if err != nil {
fmt.Println("Error from EdgeGateway", err)
Expand All @@ -207,8 +213,8 @@ var createEdgeGatewayCmd = &cobra.Command{
fmt.Println("Error during EdgeGateway Creation !!", err)
return
}
s.FinalMSG = "EdgeGateway resource created successfully !!"
s.Stop()
fmt.Println("EdgeGateway resource created successfully !")
},
}

Expand All @@ -231,15 +237,17 @@ var createS3Cmd = &cobra.Command{
}

// Create the bucket
s.Stop()
fmt.Println("create a bucket resource (with basic value)")
fmt.Println("bucket name: " + bucketName)
s.Restart()

_, err = c.V1.S3().CreateBucket(&s3.CreateBucketInput{Bucket: &bucketName})
if err != nil {
fmt.Println("Error from S3 Create", err)
return
}
s.FinalMSG = "Bucket resource created successfully !!"
s.Stop()
fmt.Println("Bucket resource created successfully !")
},
}
20 changes: 14 additions & 6 deletions cmd/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
var delCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"del", "rm"},
Example: exampleDelete1 + exampleDelete2,
Example: exampleDelete1 + "\n" + exampleDelete2,
Short: "Delete resource from CloudAvenue.",
}

Expand Down Expand Up @@ -64,7 +64,9 @@ var delVDCCmd = &cobra.Command{
}

for _, arg := range args {
s.Stop()
fmt.Println("delete vdc resource " + arg)
s.Restart()
vdc, err := c.V1.VDC().GetVDC(arg)
if err != nil {
fmt.Println("Error from vdc", err)
Expand All @@ -80,8 +82,8 @@ var delVDCCmd = &cobra.Command{
fmt.Println("Error during vdc Deletion !!", err)
return
}
s.FinalMSG = "vdc resource deleted " + arg + " successfully !!\n"
s.Stop()
fmt.Println("vdc resource deleted " + arg + " successfully !!")
}

},
Expand All @@ -100,15 +102,17 @@ var delS3Cmd = &cobra.Command{
}

for i, arg := range args {
fmt.Println("delete bucket resource " + arg)
s.Stop()
fmt.Println("delete bucket resource... " + arg)
s.Restart()
// Del the bucket
_, err := c.V1.S3().DeleteBucket(&s3.DeleteBucketInput{Bucket: &args[i]})
if err != nil {
fmt.Println("Error from S3 Delete", err)
return
}
s.FinalMSG = "Bucket resource deleted " + arg + " successfully !!\n"
s.Stop()
fmt.Println("Bucket resource deleted " + arg + " successfully !!")
}

},
Expand All @@ -132,7 +136,9 @@ var delEdgeGatewayCmd = &cobra.Command{
)

for _, arg := range args {
s.Stop()
fmt.Println("delete EdgeGateway resource " + arg)
s.Restart()
if uuid.IsUUIDV4(arg) {
gw, err = c.V1.EdgeGateway.GetByID(arg)
} else {
Expand All @@ -154,8 +160,8 @@ var delEdgeGatewayCmd = &cobra.Command{
fmt.Println("Error during EdgeGateway Deletion !!", err)
return
}
s.FinalMSG = "EdgeGateway resource deleted " + arg + " successfully !!\n"
s.Stop()
fmt.Println("EdgeGateway resource deleted " + arg + " successfully !!")
}

},
Expand All @@ -174,7 +180,9 @@ var delPublicIPCmd = &cobra.Command{
}

for _, arg := range args {
s.Stop()
fmt.Println("delete publicip resource " + arg)
s.Restart()
ip, err := c.V1.PublicIP.GetIP(arg)
if err != nil {
fmt.Println("Error from ip: ", err)
Expand All @@ -190,8 +198,8 @@ var delPublicIPCmd = &cobra.Command{
fmt.Println("Error during ip Deletion !!", err)
return
}
s.FinalMSG = "ip resource deleted " + arg + " successfully !!\n"
s.Stop()
fmt.Println("ip resource deleted " + arg + " successfully !!")
}

},
Expand Down
6 changes: 4 additions & 2 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

var (
exampleGet1 = `
#List all T0\ncav get t0`
#List all T0
cav get t0`
exampleGet2 = `
#List all T0 in wide format
cav get t0 -o wide`
Expand All @@ -22,7 +23,7 @@ var (
var getCmd = &cobra.Command{
Use: "get",
Aliases: []string{"ls", "list"},
Example: exampleGet1 + exampleGet2,
Example: exampleGet1 + "\n" + exampleGet2,
Short: "Get resource to retrieve information from CloudAvenue.",
}

Expand Down Expand Up @@ -152,6 +153,7 @@ var getS3Cmd = &cobra.Command{
w.AddFields(*b.Name, *output.Owner.DisplayName)
}
}
s.Stop()
w.PrintTable()
},
}
Expand Down
87 changes: 72 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ package cmd
import (
"fmt"
"os"
"strconv"
"time"

"github.com/adampresley/sigint"
"github.com/briandowns/spinner"
"github.com/orange-cloudavenue/cloudavenue-sdk-go"
"github.com/mitchellh/go-homedir"
cloudavenue "github.com/orange-cloudavenue/cloudavenue-sdk-go"
clientcloudavenue "github.com/orange-cloudavenue/cloudavenue-sdk-go/pkg/clients/cloudavenue"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
c *cloudavenue.Client
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
s = spinner.New(spinner.CharSets[43], 100*time.Millisecond)
c *cloudavenue.Client
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
s = spinner.New(spinner.CharSets[43], 100*time.Millisecond)
cloudavenueOrg string
cloudavenueUsername string
cloudavenuePassword string
cloudavenueDebug bool
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -32,27 +39,77 @@ var rootCmd = &cobra.Command{

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
func Execute() (err error) {
// ctrl+c handler
sigint.ListenForSIGINT(func() {
fmt.Println("SIGINT received. Exiting...")
os.Exit(0)
})

// Set default file configuration and create it if not exist
home, err := homedir.Dir()
if err != nil {
return err
}
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(home + "/.cav")
if home == "" {
return fmt.Errorf("Error in Get HOME Directory")
}
if _, err = os.Stat(home + "/.cav/config.yaml"); os.IsNotExist(err) {
if err = os.MkdirAll(home+"/.cav", 0755); err != nil {
return err
}
viper.SetDefault("cloudavenue_username", "")
viper.SetDefault("cloudavenue_password", "")
viper.SetDefault("cloudavenue_org", "")
viper.AutomaticEnv()
viper.SetDefault("cloudavenue_debug", false)

if err = viper.SafeWriteConfig(); err != nil {
return err
}
s.FinalMSG = "Configuration file created in " + home + "/.cav/config.yaml \nPlease fill it with your credentials and re-run the command.\n"
s.Stop()
os.Exit(0)
}

// check if variable is set if not, use configuration file
if os.Getenv("CLOUDAVENUE_USERNAME") == "" || os.Getenv("CLOUDAVENUE_PASSWORD") == "" || os.Getenv("CLOUDAVENUE_ORG") == "" {
if err = viper.ReadInConfig(); err != nil {
return err
}
cloudavenueUsername = viper.GetString("cloudavenue_username")
cloudavenuePassword = viper.GetString("cloudavenue_password")
cloudavenueOrg = viper.GetString("cloudavenue_org")
cloudavenueDebug = viper.GetBool("cloudavenue_debug")
} else {
cloudavenueUsername = os.Getenv("CLOUDAVENUE_USERNAME")
cloudavenuePassword = os.Getenv("CLOUDAVENUE_PASSWORD")
cloudavenueOrg = os.Getenv("CLOUDAVENUE_ORG")
x, err := strconv.ParseBool(os.Getenv("CLOUDAVENUE_DEBUG"))
if err != nil {
return err
}
cloudavenueDebug = x
}

// Set client CloudAvenue
var err error
c, err = cloudavenue.New(cloudavenue.ClientOpts{
CloudAvenue: &clientcloudavenue.Opts{},
CloudAvenue: &clientcloudavenue.Opts{
Username: cloudavenueUsername,
Password: cloudavenuePassword,
Org: cloudavenueOrg,
Debug: cloudavenueDebug,
},
})
if err != nil {
s.Stop()
fmt.Println("Error in CloudAvenue parameter, please check your configuration (https://github.com/orange-cloudavenue/cloudavenue-cli/blob/main/docs/index.md)", err)
return err
}

// Execute root command
if err := rootCmd.Execute(); err != nil {
s.Stop()
if err = rootCmd.Execute(); err != nil {
fmt.Println("Error in Command", err)
return err
}
Expand Down Expand Up @@ -82,8 +139,8 @@ func versionCmd() *cobra.Command {
Short: "Print the version number of cav",
Long: `All software has versions. This is cav's`,
Run: func(cmd *cobra.Command, args []string) {
s.FinalMSG = "Version: " + version + "\nCommit: " + commit + "\nBuilt at: " + date + "\nBuilt by: " + builtBy
s.Stop()
fmt.Printf("Version: %s\nCommit: %s\nBuilt at: %s\nBuilt by: %s\n", version, commit, date, builtBy)
},
}
}
Loading

0 comments on commit 54f8c81

Please sign in to comment.