Skip to content

Commit

Permalink
feat: add cobra for cli parsing and remove kong (#42)
Browse files Browse the repository at this point in the history
* feat: add cobra for cli parsing, change root and add commands to cobra

* feat: switch build cmd to cobra

* feat: switch init cmd to cobra

* feat: switch logout cmd to cobra

* feat: switch login cmd to cobra

* feat: switch publish cmd to cobra

* fix: format code of changed commands

* fix: remove long usage of publish command

* fix: remove non-used description in main.go and ditch Execute error

* fix: remove unused struct previously used with kong

* fix: run go mod tidy to remove kong from go.mod

* feat: switch remove cmd to cobra

* run go mod tidy

* fix: run go mod tidy
  • Loading branch information
Codelax authored Apr 13, 2021
1 parent e82528f commit 7d787fd
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 133 deletions.
37 changes: 21 additions & 16 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ import (
"fmt"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
)

//AddCmd defines the parameters of the add command.
type AddCmd struct {
Force bool `kong:"optional,name='force',help='Ignore cache.'"`
RegistryURL string `kong:"optional,name='registry-url',help='Select specific registry to use.'"`
var addCmdFlags = ctpm.AddOptions{}

Dependencies []string `kong:"arg,help='List of dependencies to add.'"`
var addCmd = &cobra.Command{
Use: "add [dependencies...]",
Short: "Add one or more new dependency",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
addCmdFlags.Dependencies = args
err = ctpm.Add(pc, addCmdFlags)
if err != nil {
return fmt.Errorf("failed to add dependencies: %w", err)
}
return nil
},
}

//Run handles the behavior of the add command.
func (a *AddCmd) Run() error {
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
err = ctpm.Add(pc, ctpm.AddOptions{Force: a.Force, RegistryURL: a.RegistryURL, Dependencies: a.Dependencies})
if err != nil {
return fmt.Errorf("failed to add dependencies: %w", err)
}
return nil
func init() {
addCmd.Flags().BoolVarP(&addCmdFlags.Force, "force", "f", false, "Ignore cache.")
addCmd.Flags().StringVarP(&addCmdFlags.RegistryURL, "registry-url", "r", "", "Select specific registry to use.")
}
22 changes: 12 additions & 10 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"fmt"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
)

//BuildCmd defines the parameters of the build command.
type BuildCmd struct{}

//Run handles the behavior of the build command.
func (b *BuildCmd) Run() error {
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
return ctpm.AddDependenciesAndBuild(pc)
var buildCmd = &cobra.Command{
Use: "build",
Short: "Build a c3pm project",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
return ctpm.AddDependenciesAndBuild(pc)
},
}
55 changes: 35 additions & 20 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,44 @@ import (
"github.com/c3pm-labs/c3pm/cmd/input"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
"path/filepath"
)

//InitCmd defines the parameters of the init command.
type InitCmd struct {
NoTemplate bool `kong:"optional,name='no-template',help='Prevents the creation of CMake files'"`
Path string `kong:"optional,arg,name='path',help='Project path, default to working directory',default='.'"`
var initCmdFlags = ctpm.InitOptions{}

var initCmd = &cobra.Command{
Use: "init [path]",
Short: "Init a c3pm project",
Long: "Init a c3pm project\n\n" +
"Project path defaults to working directory",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("requires one or no arguments")
} else if len(args) == 0 {
cmd.SetArgs([]string{"."})
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
path := args[0]
manifest, err := input.Init()
if err != nil {
return fmt.Errorf("failed to init project config: %w", err)
}
projectRoot, err := filepath.Abs(path)
if err != nil {
return err
}
pc := &config.ProjectConfig{Manifest: manifest, ProjectRoot: projectRoot}
err = ctpm.Init(pc, initCmdFlags)
if err != nil {
return fmt.Errorf("failed to init project: %w", err)
}
return nil
},
}

//Run handles the behavior of the init command.
func (i *InitCmd) Run() error {
manifest, err := input.Init()
if err != nil {
return fmt.Errorf("failed to init project config: %w", err)
}
projectRoot, err := filepath.Abs(i.Path)
if err != nil {
return err
}
pc := &config.ProjectConfig{Manifest: manifest, ProjectRoot: projectRoot}
err = ctpm.Init(pc, ctpm.InitOptions{NoTemplate: i.NoTemplate})
if err != nil {
return fmt.Errorf("failed to init project: %w", err)
}
return nil
func init() {
initCmd.Flags().BoolVar(&initCmdFlags.NoTemplate, "no-template", ctpm.InitDefaultOptions.NoTemplate, "Prevents the creation of CMake files")
}
24 changes: 13 additions & 11 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import (
"github.com/c3pm-labs/c3pm/api"
"github.com/c3pm-labs/c3pm/cmd/input"
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
"net/http"
)

//LoginCmd defines the parameters of the login command.
type LoginCmd struct{}

//Run handles the behavior of the login command.
func (l *LoginCmd) Run() error {
payload, err := input.Login()
if err != nil {
return err
}
client := api.New(&http.Client{}, "")
return ctpm.Login(client, payload.Login, payload.Password)
var loginCmd = &cobra.Command{
Use: "login",
Short: "Login to the api",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
payload, err := input.Login()
if err != nil {
return err
}
client := api.New(&http.Client{}, "")
return ctpm.Login(client, payload.Login, payload.Password)
},
}
18 changes: 11 additions & 7 deletions cmd/logout.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cmd

import "github.com/c3pm-labs/c3pm/ctpm"
import (
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
)

//LogoutCmd defines the parameters of the logout command.
type LogoutCmd struct{}

//Run handles the behavior of the logout command.
func (l *LogoutCmd) Run() error {
return ctpm.Logout()
var logoutCmd = &cobra.Command{
Use: "logout",
Short: "Logout from the api",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return ctpm.Logout()
},
}
32 changes: 16 additions & 16 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import (
"github.com/c3pm-labs/c3pm/api"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
"net/http"
)

//PublishCmd defines the parameters of the publish command.
type PublishCmd struct {
}

//Run handles the behavior of the publish command.
func (p *PublishCmd) Run() error {
token, err := config.TokenStrict()
if err != nil {
return fmt.Errorf("not logged in: %w", err)
}
client := api.New(&http.Client{}, token)
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
return ctpm.Publish(pc, client)
var publishCmd = &cobra.Command{
Use: "publish",
Short: "Publish a c3pm project",
RunE: func(cmd *cobra.Command, args []string) error {
token, err := config.TokenStrict()
if err != nil {
return fmt.Errorf("not logged in: %w", err)
}
client := api.New(&http.Client{}, token)
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
return ctpm.Publish(pc, client)
},
}
33 changes: 17 additions & 16 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import (
"fmt"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/ctpm"
"github.com/spf13/cobra"
)

//RemoveCmd defines the parameters of the remove command.
type RemoveCmd struct {
Dependencies []string `kong:"arg,help='List of dependencies to remove.'"`
}

//Run handles the behavior of the remove command.
func (a *RemoveCmd) Run() error {
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
err = ctpm.Remove(pc, ctpm.RemoveOptions{Dependencies: a.Dependencies})
if err != nil {
return fmt.Errorf("failed to remove dependencies: %w", err)
}
return nil
var removeCmd = &cobra.Command{
Use: "remove [dependencies]",
Short: "Remove one or more dependency",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dependencies := args
pc, err := config.Load(".")
if err != nil {
return fmt.Errorf("failed to read c3pm.yml: %w", err)
}
err = ctpm.Remove(pc, ctpm.RemoveOptions{Dependencies: dependencies})
if err != nil {
return fmt.Errorf("failed to remove dependencies: %w", err)
}
return nil
},
}
28 changes: 17 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
// Package cmd hosts the configuration and handling of the command line interface of C3PM.
package cmd

import "github.com/alecthomas/kong"
import (
"github.com/spf13/cobra"
)

//CLI is the root configuration of C3PM's command line interface.
var CLI struct {
Version kong.VersionFlag `short:"v" help:"outputs the version number"`
Add AddCmd `kong:"cmd,help='Add a new dependency'"`
Remove RemoveCmd `kong:"cmd,help='Remove a dependency'"`
Init InitCmd `kong:"cmd,help='Init a c3pm project'"`
Logout LogoutCmd `kong:"cmd,help='Logout from the api'"`
Login LoginCmd `kong:"cmd,help='Login to the api'"`
Build BuildCmd `kong:"cmd,help='Build a c3pm project'"`
Publish PublishCmd `kong:"cmd,help='Publish a c3pm project'"`
var RootCmd = &cobra.Command{
Use: "ctpm",
Short: "c3pm abstracts your build system and eases the management of your dependencies.",
Long: "C3PM is a next-generation package manager for C++.\nYou can use C3PM to share and use packages with other developers around the world.",
}

func init() {
RootCmd.AddCommand(addCmd)
RootCmd.AddCommand(buildCmd)
RootCmd.AddCommand(initCmd)
RootCmd.AddCommand(logoutCmd)
RootCmd.AddCommand(loginCmd)
RootCmd.AddCommand(publishCmd)
RootCmd.AddCommand(removeCmd)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ go 1.14
require (
github.com/AlecAivazis/survey/v2 v2.2.5
github.com/Masterminds/semver/v3 v3.1.1
github.com/alecthomas/kong v0.2.12
github.com/bmatcuk/doublestar v1.3.4
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/go-spdx v0.1.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/onsi/ginkgo v1.15.2
github.com/onsi/gomega v1.11.0
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/cobra v1.1.3
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9 // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/text v0.3.4 // indirect
Expand Down
Loading

0 comments on commit 7d787fd

Please sign in to comment.