Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
build/dist

# .DS_Store file of MacOS
.DS_Store
.DS_Store

# test data files
**/testdata/**
8 changes: 4 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
)

var rootCmd = &cobra.Command{
Use: "microcks",
Short: "A CLI tool for Microcks",
Long: `microcks-cli is a CLI for interacting with Microcks server APIs.
It allows to launch tests or import API artifacts with minimal dependencies.`,
Use: "microcks",
Short: "A CLI tool for Microcks",
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
Expand All @@ -48,4 +47,5 @@ func init() {
rootCmd.AddCommand(NewImportURLCommand())
rootCmd.AddCommand(NewStartCommand())
rootCmd.AddCommand(NewStopCommand())
rootCmd.AddCommand(NewContextCommand())
}
126 changes: 126 additions & 0 deletions cmd/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"
"text/tabwriter"

"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/errors"
"github.com/spf13/cobra"
)

func NewContextCommand() *cobra.Command {
var delete bool
ctxCmd := &cobra.Command{
Use: "context [CONTEXT]",
Aliases: []string{"ctx"},
Short: "switch between contexts",
Example: `# List Microcks context
microcks context/ctx

#Switch Microcks context
microcks context httP://localhost:8080

# Delete Microcks context
microcks context httP://localhost:8080 --delete`,
Run: func(cmd *cobra.Command, args []string) {
var cfgFile string
configPath, err := config.DefaultLocalConfigPath()
errors.CheckError(err)
cfgFile = configPath
localCfg, err := config.ReadLocalConfig(cfgFile)
errors.CheckError(err)
if delete {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
err := deleteContext(args[0], cfgFile)
errors.CheckError(err)
return
}

if len(args) == 0 {
printMicrocksContexts(cfgFile)
return
}

ctxName := args[0]
if localCfg.CurrentContext == ctxName {
fmt.Printf("Already at context '%s'\n", localCfg.CurrentContext)
return
}
if _, err = localCfg.ResolveContext(ctxName); err != nil {
log.Fatal(err)
}
localCfg.CurrentContext = ctxName
err = config.WriteLocalConfig(*localCfg, configPath)
errors.CheckError(err)
fmt.Printf("Switched to context '%s'\n", localCfg.CurrentContext)
},
}

ctxCmd.Flags().BoolVar(&delete, "delete", false, "Delete a context")

return ctxCmd
}

func deleteContext(context, configPath string) error {
localCfg, err := config.ReadLocalConfig(configPath)
errors.CheckError(err)
if localCfg == nil {
return fmt.Errorf("Nothing to logout from")
}
serverName, ok := localCfg.RemoveContext(context)
if !ok {
return fmt.Errorf("Context %s does not exist", context)
}
_ = localCfg.RemoveUser(context)
_ = localCfg.RemoveServer(serverName)

if localCfg.IsEmpty() {
err := localCfg.DeleteLocalConfig(configPath)
errors.CheckError(err)
} else {
if localCfg.CurrentContext == context {
localCfg.CurrentContext = ""
}
err = config.ValidateLocalConfig(*localCfg)
if err != nil {
return fmt.Errorf("Error in logging out")
}
err = config.WriteLocalConfig(*localCfg, configPath)
errors.CheckError(err)
}
fmt.Printf("Context '%s' deleted\n", context)
return nil
}

func printMicrocksContexts(configPath string) {
localCfg, err := config.ReadLocalConfig(configPath)
errors.CheckError(err)
if localCfg == nil {
log.Fatalf("No contexts defined in %s", configPath)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
defer func() { _ = w.Flush() }()
columnNames := []string{"CURRENT", "NAME", "SERVER"}
_, err = fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))
errors.CheckError(err)

for _, contextRef := range localCfg.Contexts {
context, err := localCfg.ResolveContext(contextRef.Name)
if err != nil {
log.Printf("Context '%s' had error: %v", contextRef.Name, err)
}
prefix := " "
if localCfg.CurrentContext == context.Name {
prefix = "*"
}
_, err = fmt.Fprintf(w, "%s\t%s\t%s\n", prefix, context.Name, context.Server.Server)
errors.CheckError(err)
}
}
66 changes: 66 additions & 0 deletions cmd/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"os"
"testing"

"github.com/microcks/microcks-cli/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const testConfig = `current-context: http://localhost:8083
contexts:
- name: http://localhost:8080
server: http://localhost:8080
user: http://localhost:8080
instance: ""
- name: http://localhost:8083
server: http://localhost:8083
user: http://localhost:8083
instance: ""
servers:
- name: ""
server: http://localhost:8080
insecureTLS: true
keycloakEnable: true
- name: ""
server: http://localhost:8083
insecureTLS: true
keycloakEnable: true
users:
- name: http://localhost:8080
auth-token: vErrYS3c3tReFRe$hToken
refresh-token: vErrYS3c3tReFRe$hToken
- name: http://localhost:8083
auth-token: ""
refresh-token: ""`

const testConfigFilePath = "./testdata/local.config"

func TestDeleteContext(t *testing.T) {
//write the test config file
err := os.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm)
require.NoError(t, err)

err = os.Chmod(testConfigFilePath, 0o600)
require.NoError(t, err, "Could not change the file permission to 0600 %v", err)
localCfg, err := config.ReadLocalConfig(testConfigFilePath)
require.NoError(t, err)
assert.Equal(t, "http://localhost:8083", localCfg.CurrentContext)
assert.Contains(t, localCfg.Contexts, config.ContextRef{Name: "http://localhost:8083", Server: "http://localhost:8083", User: "http://localhost:8083", Instance: ""})

//Delete non-existing context
err = deleteContext("microcks.io", testConfigFilePath)
require.EqualError(t, err, "Context microcks.io does not exist")

//Delete non-current context
err = deleteContext("http://localhost:8080", testConfigFilePath)
require.NoError(t, err)

//Delete current context
err = deleteContext("http://localhost:8083", testConfigFilePath)
require.NoError(t, err)
_, err = config.ReadLocalConfig(testConfigFilePath)
require.NoError(t, err)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ require (
github.com/docker/docker v28.0.4+incompatible
github.com/docker/go-connections v0.5.0
github.com/spf13/cobra v1.9.1
github.com/stretchr/testify v1.10.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand All @@ -27,6 +29,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
Expand All @@ -36,5 +39,6 @@ require (
go.opentelemetry.io/otel/trace v1.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/time v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.2 // indirect
)
16 changes: 16 additions & 0 deletions pkg/config/file_permission_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !windows

package config

import (
"fmt"
"os"
)

func getFilePermission(fi os.FileInfo) error {
if fi.Mode().Perm() == 0o600 || fi.Mode().Perm() == 0o400 {
return nil
}
return fmt.Errorf("config file has incorrect permission flags:%s."+
"change the file permission either to 0400 or 0600.", fi.Mode().Perm().String())
}
16 changes: 16 additions & 0 deletions pkg/config/file_permission_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build windows

package config

import (
"fmt"
"os"
)

func getFilePermission(fi os.FileInfo) error {
if fi.Mode().Perm() == 0666 || fi.Mode().Perm() == 0444 {
return nil
}
return fmt.Errorf("config file has incorrect permission flags:%s."+
"change the file permission either to 0444 or 0666.", fi.Mode().Perm().String())
}
Loading
Loading