2022-07-15 11:49:53 +00:00
|
|
|
/*
|
2022-07-16 15:46:09 +00:00
|
|
|
Copyright © 2022 Matthieu Derasse <git@derasse.fr>
|
2022-07-15 11:49:53 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-07-16 22:16:29 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.home.m-and-m.ovh/mderasse/boot/helpers"
|
2022-07-15 11:49:53 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// initCmd represents the init command
|
|
|
|
var initCmd = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize a new project",
|
|
|
|
Long: `Initialize a new project by creating a new directory and:
|
|
|
|
- Initialize a Git Repository
|
|
|
|
- Create a Go-Swagger Struct
|
|
|
|
- Add default routes
|
|
|
|
- Generate the API.
|
|
|
|
|
|
|
|
That command is Interactive or can be control through args`,
|
|
|
|
Run: runInitAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(initCmd)
|
|
|
|
}
|
|
|
|
|
2022-07-16 22:16:29 +00:00
|
|
|
// runInitAction
|
|
|
|
// Steps:
|
|
|
|
// 1 - Check that we have the dependencies
|
|
|
|
// 2 - Check that we are not in project directory
|
|
|
|
// 3 - Ask info to the user
|
2022-07-15 11:49:53 +00:00
|
|
|
func runInitAction(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
log.Debugf("Starting command Init")
|
|
|
|
|
|
|
|
log.Debugf("Checking dependecies")
|
|
|
|
|
2022-07-16 22:16:29 +00:00
|
|
|
if !checkDependencies() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current path
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to get current path. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Working in directory: %s", currentPath)
|
|
|
|
|
|
|
|
// Check if we are in goguik directory
|
|
|
|
log.Debug("Checking if we are in the same directory as Goguik")
|
|
|
|
|
|
|
|
isGoguikDir, err := helpers.IsGoguikDirectory(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to check if we are in goguik directory. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isGoguikDir {
|
|
|
|
log.Error("You cannot initialize a new project in the same dir than goguik\nGoguik should be added to your path")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are in a go project
|
|
|
|
log.Debug("Checking if we are in a golang project")
|
|
|
|
|
|
|
|
isGoProject, err := helpers.IsGoProject(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to check if we are in a go project. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isGoProject {
|
|
|
|
log.Error("You cannot initialize a new project in an already golang project")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are in a goguik project
|
|
|
|
log.Debug("Checking if we are in a goguik project")
|
|
|
|
|
|
|
|
isGoguikProject, err := helpers.IsGoguikProject(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to check if we are in a goguik project. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isGoguikProject {
|
|
|
|
log.Error("You cannot initialize a new project in an already goguik project")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX:
|
|
|
|
// Check we are in gopath or ask for the gomod name
|
|
|
|
// Create directory
|
|
|
|
// Move to dir
|
|
|
|
// git init
|
|
|
|
// create .goguik
|
|
|
|
// move templated file
|
2022-07-15 11:49:53 +00:00
|
|
|
|
|
|
|
// if isGoProject, err := helpers.IsGoProject(); err != nil {
|
|
|
|
// log.Error("An error occured when checking your directory.")
|
|
|
|
// return
|
|
|
|
// } else if isGoProject {
|
|
|
|
// log.Error("Cannot initialize a new project because you are already in a Golang Project Directory.")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if isStrapProject, err := helpers.IsStrapProject(); err != nil {
|
|
|
|
// log.Error("An error occured when checking your directory.")
|
|
|
|
// return
|
|
|
|
// } else if isStrapProject {
|
|
|
|
// log.Error("Cannot initialize a new project because you are already in a Strap Project Directory.")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
}
|