github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/cmd/pyroscope/command/admin.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/spf13/cobra" 8 9 "github.com/pyroscope-io/pyroscope/pkg/admin" 10 "github.com/pyroscope-io/pyroscope/pkg/cli" 11 "github.com/pyroscope-io/pyroscope/pkg/config" 12 ) 13 14 // admin 15 func newAdminCmd(cfg *config.Admin) *cobra.Command { 16 vpr := newViper() 17 18 var cmd *cobra.Command 19 cmd = &cobra.Command{ 20 Use: "admin", 21 Short: "administration commands", 22 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error { 23 fmt.Println(cfg) 24 printUsageMessage(cmd) 25 return nil 26 }), 27 } 28 29 // admin 30 cmd.AddCommand(newAdminAppCmd(cfg)) 31 cmd.AddCommand(newAdminUserCmd(cfg)) 32 cmd.AddCommand(newAdminStorageCmd(cfg)) 33 34 return cmd 35 } 36 37 // admin app 38 func newAdminAppCmd(cfg *config.Admin) *cobra.Command { 39 vpr := newViper() 40 41 var cmd *cobra.Command 42 cmd = &cobra.Command{ 43 Use: "app", 44 Short: "", 45 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error { 46 printUsageMessage(cmd) 47 return nil 48 }), 49 } 50 51 cmd.AddCommand(newAdminAppGetCmd(&cfg.AdminAppGet)) 52 cmd.AddCommand(newAdminAppDeleteCmd(&cfg.AdminAppDelete)) 53 54 return cmd 55 } 56 57 // admin app get 58 func newAdminAppGetCmd(cfg *config.AdminAppGet) *cobra.Command { 59 vpr := newViper() 60 cmd := &cobra.Command{ 61 Use: "get [flags]", 62 Short: "get the list of all apps", 63 Long: "get the list of all apps", 64 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error { 65 cli, err := admin.NewCLI(cfg.SocketPath, cfg.Timeout) 66 if err != nil { 67 return err 68 } 69 70 return cli.GetAppsNames() 71 }), 72 } 73 74 cli.PopulateFlagSet(cfg, cmd.Flags(), vpr) 75 return cmd 76 } 77 78 // admin app delete 79 func newAdminAppDeleteCmd(cfg *config.AdminAppDelete) *cobra.Command { 80 vpr := newViper() 81 cmd := &cobra.Command{ 82 Use: "delete [flags] [app_name]", 83 Short: "delete an app", 84 Long: "delete an app", 85 Args: cobra.ExactArgs(1), 86 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 87 if len(args) != 0 { 88 return nil, cobra.ShellCompDirectiveNoFileComp 89 } 90 91 cli, err := admin.NewCLI(cfg.SocketPath, time.Second*2) 92 if err != nil { 93 return nil, cobra.ShellCompDirectiveNoFileComp 94 } 95 96 appNames, err := cli.CompleteApp(toComplete) 97 if err != nil { 98 return nil, cobra.ShellCompDirectiveNoFileComp 99 } 100 101 return appNames, cobra.ShellCompDirectiveNoFileComp 102 }, 103 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, arg []string) error { 104 cli, err := admin.NewCLI(cfg.SocketPath, cfg.Timeout) 105 if err != nil { 106 return err 107 } 108 109 return cli.DeleteApp(arg[0], cfg.Force) 110 }), 111 } 112 113 cli.PopulateFlagSet(cfg, cmd.Flags(), vpr) 114 return cmd 115 } 116 117 func newAdminUserCmd(cfg *config.Admin) *cobra.Command { 118 vpr := newViper() 119 120 var cmd *cobra.Command 121 cmd = &cobra.Command{ 122 Use: "user", 123 Short: "manage users", 124 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error { 125 printUsageMessage(cmd) 126 return nil 127 }), 128 } 129 130 cmd.AddCommand(newAdminPasswordResetCmd(&cfg.AdminUserPasswordReset)) 131 132 return cmd 133 } 134 135 func newAdminPasswordResetCmd(cfg *config.AdminUserPasswordReset) *cobra.Command { 136 vpr := newViper() 137 cmd := &cobra.Command{ 138 Use: "reset-password [flags]", 139 Short: "reset user password", 140 Args: cobra.NoArgs, 141 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, arg []string) error { 142 ac, err := admin.NewCLI(cfg.SocketPath, cfg.Timeout) 143 if err != nil { 144 return err 145 } 146 if err = ac.ResetUserPassword(cfg.Username, cfg.Password, cfg.Enable); err != nil { 147 return err 148 } 149 fmt.Println("Password for user", cfg.Username, "has been reset successfully.") 150 return nil 151 }), 152 } 153 154 cli.PopulateFlagSet(cfg, cmd.Flags(), vpr) 155 return cmd 156 } 157 158 func newAdminStorageCmd(cfg *config.Admin) *cobra.Command { 159 vpr := newViper() 160 161 var cmd *cobra.Command 162 cmd = &cobra.Command{ 163 Use: "storage", 164 Short: "", 165 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error { 166 printUsageMessage(cmd) 167 return nil 168 }), 169 } 170 171 cmd.AddCommand(newAdminStorageCleanupCmd(&cfg.AdminStorageCleanup)) 172 return cmd 173 } 174 175 func newAdminStorageCleanupCmd(cfg *config.AdminStorageCleanup) *cobra.Command { 176 vpr := newViper() 177 cmd := &cobra.Command{ 178 Use: "cleanup", 179 Short: "remove malformed data", 180 Args: cobra.NoArgs, 181 RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, arg []string) error { 182 ac, err := admin.NewCLI(cfg.SocketPath, cfg.Timeout) 183 if err != nil { 184 return err 185 } 186 return ac.CleanupStorage() 187 }), 188 } 189 190 cli.PopulateFlagSet(cfg, cmd.Flags(), vpr) 191 return cmd 192 }