github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/orbctl/root.go (about) 1 package main 2 3 import ( 4 "context" 5 "os" 6 7 "github.com/spf13/cobra" 8 9 "github.com/caos/orbos/internal/helpers" 10 "github.com/caos/orbos/mntr" 11 "github.com/caos/orbos/pkg/git" 12 "github.com/caos/orbos/pkg/orb" 13 orbcfg "github.com/caos/orbos/pkg/orb" 14 ) 15 16 type RootValues struct { 17 Ctx context.Context 18 Monitor mntr.Monitor 19 Gitops bool 20 OrbConfig *orbcfg.Orb 21 Kubeconfig string 22 GitClient *git.Client 23 ErrFunc errFunc 24 } 25 26 type GetRootValues func(command, component string, tags map[string]interface{}) *RootValues 27 28 type errFunc func(err error) 29 30 func RootCommand() (*cobra.Command, GetRootValues) { 31 32 ctx := context.Background() 33 rv := &RootValues{ 34 Ctx: ctx, 35 ErrFunc: func(err error) { 36 if err != nil { 37 monitor.Error(err) 38 os.Exit(1) 39 } 40 }, 41 } 42 43 var ( 44 orbConfigPath string 45 verbose bool 46 disableAnalytics bool 47 ) 48 49 cmd := &cobra.Command{ 50 Use: "orbctl [flags]", 51 Short: "Interact with your orbs", 52 Long: `orbctl launches orbiters, booms networking-operators and simplifies common tasks such as updating your kubeconfig. 53 Participate in our community on https://github.com/caos/orbos 54 and visit our website at https://caos.ch`, 55 Example: `$ # For being able to use the --gitops flag, you need to create an orbconfig and add an SSH deploy key to your github project 56 $ # Create an ssh key pair 57 $ ssh-keygen -b 2048 -t rsa -f ~/.ssh/myorbrepo -q -N "" 58 $ # Create the orbconfig 59 $ mkdir -p ~/.orb 60 $ cat > ~/.orb/myorb << EOF 61 > # this is the ssh URL to your git repository 62 > url: git@github.com:me/my-orb.git 63 > masterkey: "$(openssl rand -base64 21)" # used for encrypting and decrypting secrets 64 > # the repokey is used to connect to your git repository 65 > repokey: | 66 > $(cat ~/.ssh/myorbrepo | sed s/^/\ \ /g) 67 > EOF 68 $ orbctl --gitops -f ~/.orb/myorb [command] 69 `, 70 SilenceErrors: true, 71 } 72 73 flags := cmd.PersistentFlags() 74 flags.StringVarP(&orbConfigPath, "orbconfig", "f", "~/.orb/config", "Path to the file containing the orbs git repo URL, deploy key and the master key for encrypting and decrypting secrets") 75 flags.StringVarP(&rv.Kubeconfig, "kubeconfig", "k", "~/.kube/config", "Path to the kubeconfig file to the cluster orbctl should target") 76 flags.BoolVar(&rv.Gitops, "gitops", false, "Run orbctl in gitops mode. Not specifying this flag is only supported for BOOM and Networking Operator") 77 flags.BoolVar(&verbose, "verbose", false, "Print debug levelled logs") 78 flags.BoolVar(&disableAnalytics, "disable-analytics", false, "Don't help CAOS Ltd. to improve ORBOS by sending them errors and usage data") 79 80 return cmd, func(command, component string, tags map[string]interface{}) *RootValues { 81 82 if verbose { 83 monitor = monitor.Verbose() 84 } 85 rv.Monitor = monitor 86 rv.Kubeconfig = helpers.PruneHome(rv.Kubeconfig) 87 rv.GitClient = git.New(ctx, monitor, "orbos", "orbos@caos.ch") 88 89 if rv.Gitops { 90 prunedPath := helpers.PruneHome(orbConfigPath) 91 // ignore parse error here 92 rv.OrbConfig, _ = orb.ParseOrbConfig(prunedPath) 93 if rv.OrbConfig == nil { 94 rv.OrbConfig = &orb.Orb{Path: prunedPath} 95 } 96 } 97 98 env := "unknown" 99 if orbID, err := rv.OrbConfig.ID(); err == nil { 100 env = orbID 101 } 102 103 if component == "" { 104 component = "orbctl" 105 } 106 107 if !disableAnalytics { 108 if err := mntr.Ingest(rv.Monitor, "orbos", version, env, component); err != nil { 109 panic(err) 110 } 111 } 112 113 rv.Monitor.WithFields(map[string]interface{}{"command": command, "gitops": rv.Gitops}).WithFields(tags).CaptureMessage("orbctl invoked") 114 115 return rv 116 } 117 }