github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/cli/tools/users.go (about)

     1  package tools
     2  
     3  import (
     4  	"context"
     5  	"github.com/bep/simplecobra"
     6  	"github.com/esnet/gdg/cli/support"
     7  	"github.com/esnet/gdg/internal/config"
     8  	"github.com/spf13/cobra"
     9  	"log/slog"
    10  )
    11  
    12  func newUserCommand() simplecobra.Commander {
    13  	return &support.SimpleCommand{
    14  		NameP: "users",
    15  		Short: "Manage users",
    16  		Long:  "Provides some utility to manage grafana users from the CLI.  Please note, as the credentials cannot be imported, the export with generate a default password for any user not already present",
    17  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    18  			return cd.CobraCommand.Help()
    19  
    20  		},
    21  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    22  			cmd.Aliases = []string{"u", "user"}
    23  		},
    24  		InitCFunc:    nil,
    25  		CommandsList: []simplecobra.Commander{newPromoteUserCmd()},
    26  	}
    27  
    28  }
    29  
    30  func newPromoteUserCmd() simplecobra.Commander {
    31  	return &support.SimpleCommand{
    32  		NameP: "makeGrafanaAdmin",
    33  		Short: "Promote User to Grafana Admin",
    34  		Long:  "Promote User to Grafana Admin",
    35  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    36  			slog.Info("Promoting User to Grafana Admin for context: '%s'", "context", config.Config().GetGDGConfig().GetContext())
    37  			userLogin, _ := cd.CobraCommand.Flags().GetString("user")
    38  
    39  			msg, err := rootCmd.GrafanaSvc().PromoteUser(userLogin)
    40  			if err != nil {
    41  				slog.Error(err.Error())
    42  			} else {
    43  				slog.Info(msg)
    44  				slog.Info("Please note user is a grafana admin, not necessarily an Org admin.  You may need to promote yourself manually per org")
    45  			}
    46  			return nil
    47  
    48  		},
    49  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    50  			cmd.Aliases = []string{"godmode", "promote"}
    51  			cmd.Flags().StringP("user", "u", "", "user email")
    52  			err := cmd.MarkFlagRequired("user")
    53  			if err != nil {
    54  				slog.Debug("Failed to mark user flag as required")
    55  			}
    56  		},
    57  		InitCFunc:    nil,
    58  		CommandsList: nil,
    59  	}
    60  }