github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/cmd/remote.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/criteo/command-launcher/internal/backend"
     8  	"github.com/criteo/command-launcher/internal/config"
     9  	"github.com/criteo/command-launcher/internal/context"
    10  	log "github.com/sirupsen/logrus"
    11  	"github.com/spf13/cobra"
    12  	"github.com/spf13/viper"
    13  )
    14  
    15  func AddRemoteCmd(rootCmd *cobra.Command, appCtx context.LauncherContext, back backend.Backend) {
    16  	remoteCmd := &cobra.Command{
    17  		Use:   "remote",
    18  		Short: "Manage command launcher remotes",
    19  		Long:  "Manage command launcher remotes",
    20  		RunE: func(cmd *cobra.Command, args []string) error {
    21  			if len(args) == 0 {
    22  				cmd.Help()
    23  			}
    24  			return nil
    25  		},
    26  	}
    27  
    28  	remoteListCmd := &cobra.Command{
    29  		Use:   "list",
    30  		Short: "List command launcher remotes",
    31  		Long:  "List command launcher remotes",
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			allRemotes := getAllRemotes()
    34  			for _, v := range allRemotes {
    35  				fmt.Printf("%-15s : %s\n", v.Name, v.RemoteBaseUrl)
    36  			}
    37  			return nil
    38  		},
    39  		ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    40  			return []string{}, cobra.ShellCompDirectiveNoFileComp
    41  		},
    42  	}
    43  
    44  	remoteDeleteCmd := &cobra.Command{
    45  		Use:   "delete [remote url]",
    46  		Short: "Delete command launcher remote",
    47  		Long:  "Delete command launcher remote",
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			if len(args) < 1 {
    50  				cmd.Help()
    51  				return nil
    52  			}
    53  			if args[0] == "default" {
    54  				return fmt.Errorf("can't delete the default remote repository")
    55  			}
    56  			config.RemoveRemote(args[0])
    57  			if err := viper.WriteConfig(); err != nil {
    58  				log.Error("cannot write the default configuration: ", err)
    59  				return err
    60  			}
    61  			return nil
    62  		},
    63  		ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    64  			if len(args) >= 1 {
    65  				return []string{}, cobra.ShellCompDirectiveNoFileComp
    66  			}
    67  			remotes := getAllRemotes()
    68  			remoteNames := []string{}
    69  			for _, remote := range remotes {
    70  				remoteNames = append(remoteNames,
    71  					fmt.Sprintf("%s\t%s", remote.Name, remote.RemoteBaseUrl),
    72  				)
    73  			}
    74  			return remoteNames, cobra.ShellCompDirectiveNoFileComp
    75  		},
    76  	}
    77  
    78  	remoteAddCmd := &cobra.Command{
    79  		Use:   "add [remote name] [remote base url]",
    80  		Short: "add command launcher remote",
    81  		Long:  "add command launcher remote",
    82  		RunE: func(cmd *cobra.Command, args []string) error {
    83  			if len(args) < 2 {
    84  				cmd.Help()
    85  				return nil
    86  			}
    87  			if args[0] == "default" {
    88  				return fmt.Errorf("can't add remote named 'default', it is a reserved remote name")
    89  			}
    90  			repoDir := filepath.Join(config.AppDir(), args[0])
    91  			if err := config.AddRemote(args[0], repoDir, args[1], "always"); err != nil {
    92  				return err
    93  			}
    94  			if err := viper.WriteConfig(); err != nil {
    95  				log.Error("cannot write the default configuration: ", err)
    96  				return err
    97  			}
    98  			return nil
    99  		},
   100  		ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   101  			return []string{}, cobra.ShellCompDirectiveNoFileComp
   102  		},
   103  	}
   104  
   105  	remoteCmd.AddCommand(remoteAddCmd)
   106  	remoteCmd.AddCommand(remoteListCmd)
   107  	remoteCmd.AddCommand(remoteDeleteCmd)
   108  	rootCmd.AddCommand(remoteCmd)
   109  }
   110  
   111  func getAllRemotes() []config.ExtraRemote {
   112  	allRemoteNames := []config.ExtraRemote{
   113  		{
   114  			Name:          "default",
   115  			RemoteBaseUrl: viper.GetString(config.COMMAND_REPOSITORY_BASE_URL_KEY),
   116  			RepositoryDir: viper.GetString(config.LOCAL_COMMAND_REPOSITORY_DIRNAME_KEY),
   117  			SyncPolicy:    backend.SYNC_POLICY_ALWAYS,
   118  		},
   119  	}
   120  	remotes, _ := config.Remotes()
   121  
   122  	for _, remote := range remotes {
   123  		allRemoteNames = append(allRemoteNames, remote)
   124  	}
   125  	return allRemoteNames
   126  }