Skip to content

Commit

Permalink
Merge branch 'main' of github.com:web-of-things-open-source/tm-catalo…
Browse files Browse the repository at this point in the history
…g-cli into main
  • Loading branch information
hadjian committed Nov 28, 2023
2 parents 7c7ce7f + ed38655 commit befd573
Show file tree
Hide file tree
Showing 23 changed files with 735 additions and 70 deletions.
2 changes: 1 addition & 1 deletion cmd/create-toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var createTOCCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(createTOCCmd)
RootCmd.AddCommand(createTOCCmd)
createTOCCmd.Flags().StringP("remote", "r", "", "use named remote instead of default")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var fetchCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(fetchCmd)
RootCmd.AddCommand(fetchCmd)
fetchCmd.Flags().StringP("remote", "r", "", "use named remote instead of default")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var listCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(listCmd)
RootCmd.AddCommand(listCmd)
listCmd.Flags().StringP("remote", "r", "", "use named remote instead of default")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ file-or-dirname
}

func init() {
rootCmd.AddCommand(pushCmd)
RootCmd.AddCommand(pushCmd)
pushCmd.Flags().StringP("remote", "r", "", "use named remote instead of default")
pushCmd.Flags().StringP("opt-path", "p", "", "append optional path to mandatory target directory structure")
pushCmd.Flags().BoolP("opt-tree", "t", false, "use original directory tree as optional path for each file. Has no effect with a single file. Overrides -p")
Expand Down
37 changes: 37 additions & 0 deletions cmd/remote/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/cmd"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteCmd represents the remote command
var remoteCmd = &cobra.Command{
Use: "remote",
Short: "Manage remote repositories",
Long: `The command remote and its subcommands allow to manage the list of remote repositories and their settings.
When no subcommand is given, defaults to list.`,
Run: func(cmd *cobra.Command, args []string) {
err := cli.RemoteList()
if err != nil {
os.Exit(1)
}
},
}

func init() {
cmd.RootCmd.AddCommand(remoteCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// remoteCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// remoteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
49 changes: 49 additions & 0 deletions cmd/remote/remote_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteAddCmd represents the 'remote add' command
var remoteAddCmd = &cobra.Command{
Use: "add [--type <type>] <name> (<config> | --file <configFileName>)",
Short: "Add a remote repository",
Long: `Add a remote repository to the tm-catalog-cli configuration file. Depending on the remote type,
the config may be a simple string, like a URL, or a json file.
--type is optional only if --file is used and the type is specified there.
`,
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
typ, err := cmd.Flags().GetString("type")
if err != nil {
cli.Stderrf("internal error: %v", err)
os.Exit(1)
}
name := args[0]
confStr := ""
if len(args) > 1 {
confStr = args[1]
}

confFile, err := cmd.Flags().GetString("file")
if err != nil {
cli.Stderrf("internal error: %v", err)
os.Exit(1)
}

err = cli.RemoteAdd(name, typ, confStr, confFile)
if err != nil {
_ = cmd.Usage()
os.Exit(1)
}
},
}

func init() {
remoteCmd.AddCommand(remoteAddCmd)
remoteAddCmd.Flags().StringP("type", "t", "", "type of remote to add")
remoteAddCmd.Flags().StringP("file", "f", "", "name of the file to read remote config from")
}
26 changes: 26 additions & 0 deletions cmd/remote/remote_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteRemoveCmd represents the 'remote remove' command
var remoteRemoveCmd = &cobra.Command{
Use: "remove <name>",
Short: "Remove the named remote repository from config",
Long: `Remove the named remote repository from config`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := cli.RemoteRemove(args[0])
if err != nil {
os.Exit(1)
}
},
}

func init() {
remoteCmd.AddCommand(remoteRemoveCmd)
}
26 changes: 26 additions & 0 deletions cmd/remote/remote_rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteRenameCmd represents the 'remote show' command
var remoteRenameCmd = &cobra.Command{
Use: "rename <old-name> <new-name>",
Short: "Renames remote <old-name> to <new-name>",
Long: `Renames remote <old-name> to <new-name>`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
err := cli.RemoteRename(args[0], args[1])
if err != nil {
os.Exit(1)
}
},
}

func init() {
remoteCmd.AddCommand(remoteRenameCmd)
}
48 changes: 48 additions & 0 deletions cmd/remote/remote_set_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteSetConfigCmd represents the 'remote add' command
var remoteSetConfigCmd = &cobra.Command{
Use: "set-config [--type <type>] <name> (<config> | --file <configFileName>)",
Short: "Set config for a remote repository",
Long: `Overwrite config of a remote repository. Depending on the remote type,
the config may be a simple string, like a URL, or a json file.
--type is optional only if --file is used and the type is specified there.`,
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
typ, err := cmd.Flags().GetString("type")
if err != nil {
cli.Stderrf("internal error: %v", err)
os.Exit(1)
}
name := args[0]
confStr := ""
if len(args) > 1 {
confStr = args[1]
}

confFile, err := cmd.Flags().GetString("file")
if err != nil {
cli.Stderrf("internal error: %v", err)
os.Exit(1)
}

err = cli.RemoteSetConfig(name, typ, confStr, confFile)
if err != nil {
_ = cmd.Usage()
os.Exit(1)
}
},
}

func init() {
remoteCmd.AddCommand(remoteSetConfigCmd)
remoteSetConfigCmd.Flags().StringP("type", "t", "", "type of remote to add")
remoteSetConfigCmd.Flags().StringP("file", "f", "", "name of the file to read remote config from")
}
26 changes: 26 additions & 0 deletions cmd/remote/remote_set_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteSetDefaultCmd represents the 'remote set-default' command
var remoteSetDefaultCmd = &cobra.Command{
Use: "set-default <name>",
Short: "Set named remote repository as default",
Long: `Set named remote repository as default`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := cli.RemoteSetDefault(args[0])
if err != nil {
os.Exit(1)
}
},
}

func init() {
remoteCmd.AddCommand(remoteSetDefaultCmd)
}
26 changes: 26 additions & 0 deletions cmd/remote/remote_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package remote

import (
"os"

"github.com/spf13/cobra"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/app/cli"
)

// remoteShowCmd represents the 'remote show' command
var remoteShowCmd = &cobra.Command{
Use: "show <name>",
Short: "Shows settings for the remote <name>",
Long: `Shows settings for the remote <name>`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := cli.RemoteShow(args[0])
if err != nil {
os.Exit(1)
}
},
}

func init() {
remoteCmd.AddCommand(remoteShowCmd)
}
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "tm-catalog-cli",
Short: "A CLI client for TM catalogs",
Long: `tm-catalog-cli is a CLI client for contributing to and searching
ThingModel catalogs.`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
err := rootCmd.Execute()
err := RootCmd.Execute()
if err != nil {
os.Exit(1)
}
Expand All @@ -29,5 +29,5 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tm-catalog-cli.yaml)")
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tm-catalog-cli.yaml)")
}
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ var validateCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(validateCmd)
RootCmd.AddCommand(validateCmd)
}
2 changes: 1 addition & 1 deletion cmd/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var versionsCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(versionsCmd)
RootCmd.AddCommand(versionsCmd)
versionsCmd.Flags().StringP("remote", "r", "", "use named remote instead of default")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/app/cli/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func pushFile(filename string, remote remotes.Remote, optPath string) (PushResul
_, raw, err := internal.ReadRequiredFile(filename)
if err != nil {
Stderrf("Couldn't read file %s: %v", filename, err)
return PushResult{PushErr, err.Error()}, err
return PushResult{PushErr, fmt.Sprintf("error pushing file %s: %s", filename, err.Error())}, err
}
id, err := commands.PushFile(raw, remote, optPath)
if err != nil {
var errExists *remotes.ErrTMExists
if errors.As(err, &errExists) {
return PushResult{TMExists, fmt.Sprintf("file %s already exists as %s", filename, id.String())}, nil
}
return PushResult{PushErr, err.Error()}, err
return PushResult{PushErr, fmt.Sprintf("error pushing file %s: %s", filename, err.Error())}, err
}

return PushResult{PushOK, fmt.Sprintf("file %s pushed as %s", filename, id.String())}, nil
Expand Down
Loading

0 comments on commit befd573

Please sign in to comment.