Skip to content

Commit

Permalink
From scratch: solid project foundations
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-santoni committed Aug 30, 2019
1 parent ca7be12 commit 47c8fdf
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 240 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ctpm
.idea
c3pm.yml
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# c3pm-cli
The CLI for c3pm

The CLI for c3pm. More to come!
20 changes: 13 additions & 7 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package cmd

import (
"errors"
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"ctpm/constants"
)

// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add [dependency]",
Short: "Add a dependency to your project",
Run: func(cmd *cobra.Command, args []string) {

RunE: func(cmd *cobra.Command, args []string) error {
readConfigMandatory()

if len(args) < 1 {
log.Fatal("Usage: ctpm add [string]")
return errors.New(constants.MissingCommandArgument)
}
viper.Set("Dependencies", args[0])
fmt.Println("add called with arg :", args[0])

newDependency := args[0]
fmt.Printf("add called with: %s\n", newDependency)

return nil
},
}

Expand Down
24 changes: 0 additions & 24 deletions cmd/build.go

This file was deleted.

20 changes: 20 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/viper"

"ctpm/constants"
)

func readConfigMandatory() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
fmt.Printf(
"Could not read %s. Have you created a c3pm project?\n",
constants.ConfigurationFileName)
os.Exit(constants.ConfigurationError)
}
}
26 changes: 6 additions & 20 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,16 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Init a project",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("init called")
initProject()
},
}
Use: "add",
Short: "Initiate a new c3pm project",

func initProject() {
var text string
fmt.Print("Project name: ")
fmt.Scanln(&text)
viper.Set("name", text)
fmt.Print("Author: ")
fmt.Scanln(&text)
viper.Set("author", text)
fmt.Print("Short description (1 line): ")
fmt.Scanln(&text)
viper.Set("description", text)
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("User wants to initiate a project. This part will be interactive.")
return nil
},
}

func init() {
Expand Down
20 changes: 0 additions & 20 deletions cmd/login.go

This file was deleted.

20 changes: 0 additions & 20 deletions cmd/logout.go

This file was deleted.

24 changes: 0 additions & 24 deletions cmd/publish.go

This file was deleted.

24 changes: 0 additions & 24 deletions cmd/remove.go

This file was deleted.

24 changes: 0 additions & 24 deletions cmd/rm.go

This file was deleted.

48 changes: 21 additions & 27 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,44 @@ import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var configFile string
"github.com/spf13/cobra"

"ctpm/constants"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: os.Args[0],
Use: "ctpm",
Short: "ctpm (c3pm) is a package manager for C++",
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
err := viper.WriteConfig()
if err != nil {
os.Exit(0)
fmt.Printf("Error: %s.\n", err)
fmt.Println("Please, use the help command to know more.")

os.Exit(constants.CommandError)
}
//fmt.Println("debug")
}

func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// We silence usage here as the cmdError type
// invites the user to display the usage when displayed
rootCmd.SilenceUsage = true

// We silence the errors as we handle the printing ourselves
rootCmd.SilenceErrors = true

// Cobra also supports local flags, which will only run
// when this action is called directly.
cobra.OnInitialize(initConfig)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
configFile = "c3pm.yml"
viper.SetConfigType("yml")
viper.SetConfigFile(configFile)
// We are looking at "./c3pm.yml"
viper.AddConfigPath(".")
viper.SetEnvPrefix("c3pm")
viper.AutomaticEnv() // read in environment variables that match
viper.SetConfigType("yml")
viper.SetConfigName("c3pm")

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
// Commands are then responsible for reading the configuration file
// when needed
}
24 changes: 0 additions & 24 deletions cmd/run.go

This file was deleted.

24 changes: 0 additions & 24 deletions cmd/unpublish.go

This file was deleted.

14 changes: 14 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package constants

const (
ConfigurationFileName = "c3pm.yml"
)

const (
CommandError = 1
ConfigurationError = 2
)

const (
MissingCommandArgument = "missing command argument"
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down

0 comments on commit 47c8fdf

Please sign in to comment.