Skip to content

Commit

Permalink
feat(ui): hide admin categories and sub-commands for users
Browse files Browse the repository at this point in the history
Ticket: TE-2891
  • Loading branch information
f0086 authored and BirknerAlex committed Apr 26, 2024
1 parent 348a32f commit 3d930ff
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
9 changes: 6 additions & 3 deletions cmd/agent/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var printVersion = false
func New() *cobra.Command {
rootCmd := cobra.Command{
Use: consts.BinaryName,
Short: fmt.Sprintf("%s is the command line tool for interacting with the GP Cloud API", consts.BinaryName),
Long: fmt.Sprintf("%s is the command line tool for interacting with the GPCore API", consts.BinaryName),
Short: fmt.Sprintf("%s is the command line tool for interacting with the GPCORE API", consts.BinaryName),
Long: fmt.Sprintf("%s is the command line tool for interacting with the GPCORE API", consts.BinaryName),
RunE: func(cobraCmd *cobra.Command, args []string) error {
if printVersion {
cobraCmd.Print(cmd.GetVersionDisplay())
Expand All @@ -44,7 +44,10 @@ func New() *cobra.Command {

// Special client commands
cmd.SelfupdateCommand(&rootCmd)
cmd.LiveLogCommand(&rootCmd)

if config.HasAdminConfig() {
cmd.LiveLogCommand(&rootCmd)
}
//InteractiveCLICommand(&rootCmd)

// Autogenerated commands
Expand Down
4 changes: 2 additions & 2 deletions gpcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
//go:generate go run ./pkg/generator/generator.go
//go:generate gofmt -s -w ./cmd/

// GPCORE CLI (gpc in short) is a command line interface for the G-Portal Cloud
// GPCORE CLI (gpc in short) is a command line interface for the GPCORE
// API. It is written in Go and uses the Cobra framework. The commands are auto
// generated from definition files in the pkg/generator/definition directory
// (the generated files get the _gen postfix). Custom commands can be added in
Expand All @@ -28,7 +28,7 @@ import (
// additional logic.
//
// The client and the agent communicate via SSH. The agent is a SSH server that
// executes commands on the G-Portal Cloud API. The client is a SSH client that
// executes commands on the GPCORE API. The client is a SSH client that
// connects to the agent and executes commands on the agent. The agent is only
// listening on localhost and the SSH keypair is stored in the users home
// directory. The private key is secured with a password. This client/server
Expand Down
23 changes: 16 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ var Endpoint = client.DefaultEndpoint
var sessionConfig *SessionConfig

func HasConfig() bool {
if os.Getenv("GPCORE_CONFIG") != "" {
ConfigFilePath = os.Getenv("GPCORE_CONFIG")
}

if _, err := os.Stat(ConfigFilePath); err == nil {
return true
}
return false
}

func HasAdminConfig() bool {
return HasConfig() && sessionConfig.Username != nil && sessionConfig.Password != nil
// No session config, no admin config
config, err := GetSessionConfig()
if err != nil {
return false
}

// No username and password, no admin config
return config.Username != nil && config.Password != nil
}

func AskForCredentials() (string, string) {
Expand Down Expand Up @@ -104,10 +107,10 @@ func GetSessionConfig() (*SessionConfig, error) {
sessionConfig = &SessionConfig{}

// No config file found?
if _, err := os.Stat(ConfigFilePath); err != nil {
if !HasConfig() {
log.Errorf("No config file found at %s", ConfigFilePath)
log.Errorf("Create a new config file with \"gpcore agent setup\"")
return nil, err
return nil, errors.New("no config file found")
}

// Read in config file
Expand All @@ -119,3 +122,9 @@ func GetSessionConfig() (*SessionConfig, error) {

return sessionConfig, nil
}

func init() {
if os.Getenv("GPCORE_CONFIG") != "" {
ConfigFilePath = os.Getenv("GPCORE_CONFIG")
}
}
7 changes: 7 additions & 0 deletions pkg/generator/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package generator

import (
"fmt"
"github.com/G-PORTAL/gpcore-cli/pkg/config"
"gopkg.in/yaml.v3"
"regexp"
"strings"
)

type Action struct {
Expand All @@ -17,6 +19,11 @@ type Action struct {
Fields []string `yaml:"fields"`
}

func (action *Action) CanCall() bool {
adminCall := strings.HasPrefix(action.APICall.Client, "admin")
return !adminCall || adminCall && config.HasAdminConfig()
}

type Param struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Expand Down
22 changes: 17 additions & 5 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func main() {
log.Fatal(err)
}

commandList := []string{}

for _, definitionFile := range definitionFiles {
log.Printf("Generate subcommand as definied in %s ...\n", definitionFile.Name())
definition, err := os.ReadFile("./pkg/generator/definition/" + definitionFile.Name())
Expand All @@ -46,6 +48,12 @@ func main() {
subcommandName := strings.Replace(strings.TrimSuffix(definitionFile.Name(), filepath.Ext(definitionFile.Name())), "-", "_", -1)
metadata.Name = subcommandName

// If there are no actions defined, we can skip this definition completely
if len(metadata.Actions) == 0 {
log.Printf(" No actions defined in %s, skipping ...\n", subcommandName)
continue
}

// Create directory if not exist
if _, err := os.Stat("./cmd/" + subcommandName); os.IsNotExist(err) {
log.Printf(" Create directory ./cmd/%s ...\n", subcommandName)
Expand All @@ -65,6 +73,7 @@ func main() {
}

// Generate all subcommands
addedSubcommands := len(metadata.Actions)
for action, meta := range metadata.Actions {
// Check if the subcommand is overwritten by the user
if _, err := os.Stat("./cmd/" + subcommandName + "/" + strcase.SnakeCase(action) + ".go"); !os.IsNotExist(err) {
Expand All @@ -81,6 +90,14 @@ func main() {
if err != nil {
log.Fatal(err)
}

if !meta.CanCall() {
addedSubcommands--
}
}

if addedSubcommands > 0 {
commandList = append(commandList, subcommandName)
}
}

Expand All @@ -99,11 +116,6 @@ func main() {

// Generate the AddCommands func, so the commands get added to the root command
targetFilename := "./cmd/addcommands" + generatedFileSuffix + ".go"
commandList := []string{}
for _, definitionFile := range definitionFiles {
subcommandName := strings.TrimSuffix(definitionFile.Name(), filepath.Ext(definitionFile.Name()))
commandList = append(commandList, subcommandName)
}
err = generator.GenerateAddCommands(commandList, targetFilename)
if err != nil {
log.Fatal(err)
Expand Down
15 changes: 12 additions & 3 deletions pkg/generator/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,19 @@ func initFunc(name string, metadata SubcommandMetadata) []Code {
c = append(c, Line())
}

// Add the command to the root command
c = append(c, Id("Root"+strcase.UpperCamelCase(metadata.Definition.Name)+"Command").
// Add the command to the root command when the user has set up the admin
// configuration.
addCommand := Id("Root" + strcase.UpperCamelCase(metadata.Definition.Name) + "Command").
Dot("AddCommand").
Call(Id(name+"Cmd")))
Call(Id(name + "Cmd"))

if metadata.Action.APICall.Client == "admin" {
c = append(c, If(
Qual("github.com/G-PORTAL/gpcore-cli/pkg/config", "HasAdminConfig").Call().Block(
addCommand)))
} else {
c = append(c, addCommand)
}

return c
}
Expand Down

0 comments on commit 3d930ff

Please sign in to comment.