Skip to content

Commit

Permalink
Merge pull request #44 from latitudesh/PD-3697-CLI-Parameterize-proje…
Browse files Browse the repository at this point in the history
…ct-name-on-the-servers-creation

feat: project flag selection
  • Loading branch information
ynhummel committed Apr 30, 2024
2 parents bcd54f8 + 919b112 commit c4fed30
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 57 deletions.
28 changes: 1 addition & 27 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/latitudesh/lsh/client"
Expand All @@ -13,7 +12,6 @@ import (
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -56,7 +54,7 @@ func makeClient(cmd *cobra.Command, args []string) (*client.LatitudeShAPI, error

// MakeRootCmd returns the root cmd
func MakeRootCmd(rootCmd *cobra.Command) (*cobra.Command, error) {
cobra.OnInitialize(initViperConfigs)
lsh.InitViperConfigs()

// Edit commands template
rootCmd.SetVersionTemplate(fmt.Sprintf("lsh %s\n", rootCmd.Version))
Expand Down Expand Up @@ -143,30 +141,6 @@ func MakeRootCmd(rootCmd *cobra.Command) (*cobra.Command, error) {
return rootCmd, nil
}

// initViperConfigs initialize viper config using config file in '$HOME/.config/<cli name>/config.<json|yaml...>'
// currently hostname, scheme and auth tokens can be specified in this config file.
func initViperConfigs() {
if configFile != "" {
// use user specified config file location
viper.SetConfigFile(configFile)
} else {
// look for default config
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)

// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(path.Join(home, ".config", exeName))
viper.SetConfigName("config")
}

if err := viper.ReadInConfig(); err != nil {
lsh.LogDebugf("Error: loading config file: %v", err)
return
}
lsh.LogDebugf("Using config file: %v", viper.ConfigFileUsed())
}

// registerAuthInoWriterFlags registers all flags needed to perform authentication
func registerAuthInoWriterFlags(cmd *cobra.Command) error {
/*Authorization */
Expand Down
35 changes: 29 additions & 6 deletions cli/create_server_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ func (o *CreateServerOperation) registerFlags(cmd *cobra.Command) {
Required: true,
Options: server.SupportedPlans,
},
&cmdflag.String{
Name: "project",
Label: "Project",
Description: "The project (ID or Slug) to deploy the server",
Required: true,
},
&cmdflag.String{
Name: "site",
Label: "Site",
Expand Down Expand Up @@ -115,6 +109,18 @@ func (o *CreateServerOperation) registerFlags(cmd *cobra.Command) {
}

func (o *CreateServerOperation) preRun(cmd *cobra.Command, args []string) {
projects := fetchUserProjects()
schema := &cmdflag.FlagsSchema{
&cmdflag.String{
Name: "project",
Label: "Project",
Description: "The project (ID or Slug) to deploy the server",
Required: true,
Options: projects,
},
}
o.BodyAttributesFlags.AppendFlags(schema)

o.BodyAttributesFlags.PreRun(cmd, args)
}

Expand Down Expand Up @@ -144,3 +150,20 @@ func (o *CreateServerOperation) run(cmd *cobra.Command, args []string) error {

return nil
}

func fetchUserProjects() []string {
userProjects := []string{}
c := lsh.NewClient()

projects, _, err := c.Projects.List(nil)
if err != nil {
utils.PrintError(err)
return nil
}

for _, proj := range projects {
userProjects = append(userProjects, proj.Name)
}

return userProjects
}
18 changes: 12 additions & 6 deletions cli/create_virtual_network_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ func (o *CreateVirtualNetworkOperation) registerFlags(cmd *cobra.Command) {
Description: "Description",
Required: true,
},
&cmdflag.String{
Name: "project",
Label: "Project ID or slug",
Description: "Project ID or slug",
Required: true,
},
&cmdflag.String{
Name: "site",
Label: "Site ID or slug",
Expand All @@ -69,6 +63,18 @@ func (o *CreateVirtualNetworkOperation) registerFlags(cmd *cobra.Command) {
}

func (o *CreateVirtualNetworkOperation) preRun(cmd *cobra.Command, args []string) {
projects := fetchUserProjects()
schema := &cmdflag.FlagsSchema{
&cmdflag.String{
Name: "project",
Label: "Project ID or Slug",
Description: "Project ID or Slug",
Required: true,
Options: projects,
},
}
o.BodyAttributesFlags.AppendFlags(schema)

o.BodyAttributesFlags.PreRun(cmd, args)
}

Expand Down
14 changes: 10 additions & 4 deletions cli/delete_project_ssh_key_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@ func (o *DeleteSSHKeyOperation) registerFlags(cmd *cobra.Command) {
Description: "ID from the SSH Key you want to update",
Required: true,
},
}

o.PathParamFlags.Register(schema)
}

func (o *DeleteSSHKeyOperation) preRun(cmd *cobra.Command, args []string) {
projects := fetchUserProjects()
schema := &cmdflag.FlagsSchema{
&cmdflag.String{
Name: "project",
Label: "Project ID or Slug",
Description: "Project ID or Slug",
Required: true,
Options: projects,
},
}
o.PathParamFlags.AppendFlags(schema)

o.PathParamFlags.Register(schema)
}

func (o *DeleteSSHKeyOperation) preRun(cmd *cobra.Command, args []string) {
o.PathParamFlags.PreRun(cmd, args)
}

Expand Down
21 changes: 13 additions & 8 deletions cli/post_project_ssh_key_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@ func (o *CreateSSHKeyOperation) registerFlags(cmd *cobra.Command) {
o.PathParamFlags = cmdflag.Flags{FlagSet: cmd.Flags()}
o.BodyAttributesFlags = cmdflag.Flags{FlagSet: cmd.Flags()}

pathParamsSchema := &cmdflag.FlagsSchema{
&cmdflag.String{
Name: "project",
Label: "ID or Slug from the project you want to add the SSH Key",
Description: "ID or Slug from the project you want to add the SSH Key",
Required: true,
},
}
pathParamsSchema := &cmdflag.FlagsSchema{}

bodyFlagsSchema := &cmdflag.FlagsSchema{
&cmdflag.String{
Expand All @@ -72,6 +65,18 @@ func (o *CreateSSHKeyOperation) registerFlags(cmd *cobra.Command) {
}

func (o *CreateSSHKeyOperation) preRun(cmd *cobra.Command, args []string) {
projects := fetchUserProjects()
schema := &cmdflag.FlagsSchema{
&cmdflag.String{
Name: "project",
Label: "ID or Slug from the project you want to add the SSH Key",
Description: "ID or Slug from the project you want to add the SSH Key",
Required: true,
Options: projects,
},
}
o.PathParamFlags.AppendFlags(schema)

o.PathParamFlags.PreRun(cmd, args)
o.BodyAttributesFlags.PreRun(cmd, args)
}
Expand Down
18 changes: 12 additions & 6 deletions cli/put_project_ssh_key_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ func (o *UpdateSSHKeyOperation) registerFlags(cmd *cobra.Command) {
Description: "ID from the SSH Key you want to update",
Required: true,
},
&cmdflag.String{
Name: "project",
Label: "Project ID or Slug",
Description: "Project ID or Slug",
Required: true,
},
}

bodyFlagsSchema := &cmdflag.FlagsSchema{
Expand All @@ -78,6 +72,18 @@ func (o *UpdateSSHKeyOperation) registerFlags(cmd *cobra.Command) {
}

func (o *UpdateSSHKeyOperation) preRun(cmd *cobra.Command, args []string) {
projects := fetchUserProjects()
schema := &cmdflag.FlagsSchema{
&cmdflag.String{
Name: "project",
Label: "Project ID or Slug",
Description: "Project ID or Slug",
Required: true,
Options: projects,
},
}
o.PathParamFlags.AppendFlags(schema)

o.PathParamFlags.PreRun(cmd, args)
o.BodyAttributesFlags.PreRun(cmd, args)
}
Expand Down
27 changes: 27 additions & 0 deletions cmd/lsh/lsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
package lsh

import (
"fmt"
"log"
"path"

sdk "github.com/latitudesh/latitudesh-go"
"github.com/latitudesh/lsh/internal/version"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand All @@ -14,6 +19,10 @@ var DryRun bool
// Debug flag indicating that cli should output debug logs
var Debug bool

var UserAgent = fmt.Sprintf("Latitude-CLI: %s", version.Version)

var ExeName = "lsh"

// LogDebugf writes debug log to stdout
func LogDebugf(format string, v ...interface{}) {
if !Debug {
Expand All @@ -29,7 +38,25 @@ func NewClient() *sdk.Client {

if AuthorizationKey != "" {
c = sdk.NewClientWithAuth("latitudesh", AuthorizationKey, nil)
c.UserAgent = UserAgent
}

return c
}

func InitViperConfigs() {
// look for default config
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)

// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(path.Join(home, ".config", ExeName))
viper.SetConfigName("config")

if err := viper.ReadInConfig(); err != nil {
LogDebugf("Error: loading config file: %v", err)
return
}
LogDebugf("Using config file: %v", viper.ConfigFileUsed())
}
8 changes: 8 additions & 0 deletions internal/cmdflag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func (f *Flags) Register(s *FlagsSchema) {
}
}

func (f *Flags) AppendFlags(s *FlagsSchema) {
*f.schema = append(*f.schema, *s...)

for _, v := range *s {
v.Register(f.FlagSet)
}
}

func (f *Flags) GetInputs() []prompt.PromptInput {
var inputs []prompt.PromptInput

Expand Down
6 changes: 6 additions & 0 deletions internal/utils/json_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func AssignValue(obj interface{}, jsonName string, value interface{}) {
if fieldValue.IsValid() && fieldValue.CanSet() {
valueToSet := reflect.ValueOf(value)

// Prepare for user passing project Name instead of Slug.
if jsonName == "project" {
parametrizedProject := strings.ToLower(strings.Replace(valueToSet.String(), " ", "-", -1))
valueToSet = reflect.ValueOf(parametrizedProject)
}

fieldValue.Set(valueToSet)
}
}
Expand Down

0 comments on commit c4fed30

Please sign in to comment.