2023-03-04 22:22:03 +00:00
package cmd
import (
"log"
"os"
"strings"
"github.com/abs3ntdev/haunt/src/config"
"github.com/abs3ntdev/haunt/src/haunt"
"github.com/spf13/cobra"
)
var addConfig config . Flags
var addCmd = & cobra . Command {
2023-03-08 21:46:44 +00:00
Use : "add" ,
Aliases : [ ] string { "a" , "create" , "new" } ,
Short : "Adds a project by name" ,
Long : "Adds a project by name, if path is provided it will use 'cmd/name', all flags provided will be saved in the config file. By default go install and go run will be ran" ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) ) ,
2023-03-04 22:22:03 +00:00
ValidArgsFunction : func ( cmd * cobra . Command , args [ ] string , toComplete string ) ( [ ] string , cobra . ShellCompDirective ) {
if len ( args ) >= 1 {
return nil , cobra . ShellCompDirectiveNoFileComp
}
return getPotentialProjets ( toComplete ) , cobra . ShellCompDirectiveNoFileComp
} ,
RunE : add ,
}
func getPotentialProjets ( in string ) [ ] string {
2023-03-06 07:39:01 +00:00
h := haunt . NewHaunt ( )
err := h . Settings . Read ( & h )
2023-03-04 22:22:03 +00:00
if err != nil {
return [ ] string { }
}
out := [ ] string { }
cmdDir , err := os . ReadDir ( "cmd" )
if err != nil {
return [ ] string { }
}
for _ , dir := range cmdDir {
exists := false
2023-03-06 07:39:01 +00:00
for _ , proj := range h . Projects {
2023-03-04 22:22:03 +00:00
if dir . Name ( ) == proj . Name {
exists = true
continue
}
}
if ! exists {
if in == "" {
out = append ( out , dir . Name ( ) )
} else {
if strings . HasPrefix ( dir . Name ( ) , in ) {
out = append ( out , dir . Name ( ) )
}
}
}
}
return out
}
func init ( ) {
rootCmd . AddCommand ( addCmd )
2023-03-05 22:28:09 +00:00
addCmd . Flags ( ) . StringVarP ( & addConfig . Path , "path" , "p" , "./" , "Project base path" )
2023-03-04 22:22:03 +00:00
addCmd . Flags ( ) . BoolVarP ( & addConfig . Format , "fmt" , "f" , false , "Enable go fmt" )
addCmd . Flags ( ) . BoolVarP ( & addConfig . Vet , "vet" , "v" , false , "Enable go vet" )
addCmd . Flags ( ) . BoolVarP ( & addConfig . Test , "test" , "t" , false , "Enable go test" )
addCmd . Flags ( ) . BoolVarP ( & addConfig . Generate , "generate" , "g" , false , "Enable go generate" )
addCmd . Flags ( ) . BoolVarP ( & addConfig . Install , "install" , "i" , true , "Enable go install" )
addCmd . Flags ( ) . BoolVarP ( & addConfig . Build , "build" , "b" , false , "Enable go build" )
addCmd . Flags ( ) . BoolVarP ( & addConfig . Run , "run" , "r" , true , "Enable go run" )
}
// Add a project to an existing config or create a new one
func add ( cmd * cobra . Command , args [ ] string ) ( err error ) {
addConfig . Name = args [ 0 ]
2023-03-06 07:39:01 +00:00
h := haunt . NewHaunt ( )
2023-03-04 22:22:03 +00:00
// read a config if exist
2023-03-06 07:39:01 +00:00
err = h . Settings . Read ( & h )
2023-03-04 22:22:03 +00:00
if err != nil {
return err
}
2023-03-06 07:39:01 +00:00
projects := len ( h . Projects )
2023-03-04 22:22:03 +00:00
// create and add a new project
2023-03-06 07:39:01 +00:00
h . Add ( h . New ( addConfig ) )
if len ( h . Projects ) > projects {
2023-03-04 22:22:03 +00:00
// update config
2023-03-06 07:39:01 +00:00
err = h . Settings . Write ( h )
2023-03-04 22:22:03 +00:00
if err != nil {
return err
}
2023-03-06 07:39:01 +00:00
log . Println ( h . Prefix ( haunt . Green . Bold ( "project successfully added" ) ) )
2023-03-04 22:22:03 +00:00
} else {
2023-03-06 07:39:01 +00:00
log . Println ( h . Prefix ( haunt . Green . Bold ( "project can't be added" ) ) )
2023-03-04 22:22:03 +00:00
}
return nil
}