add create functionnality

This commit is contained in:
2023-07-01 00:49:45 -04:00
parent 03ddddd2e2
commit 71fe930228
9 changed files with 318 additions and 85 deletions
+67 -19
View File
@@ -39,6 +39,8 @@ var CreateCmd = &cobra.Command{
},
}
// The CreateProject function creates a project structure, including a directory, version control, and
// a readme file, based on the provided arguments and flags.
func CreateProject(cmd *cobra.Command, args []string) error {
godotenv.Load()
@@ -65,11 +67,70 @@ func CreateProject(cmd *cobra.Command, args []string) error {
fmt.Println("Structure Name:" + data.Name)
// Make API request to create Github project
jsonData, _ := json.Marshal(data)
// if directory flag is set, create structure in current directory
if CfgGlobal.GetCreateDirectoryFlag() {
// Create directory of the project
CreateProjectStructure(data)
// if version flag is set, create version control in project
if CfgGlobal.GetCreateVersionFlag() {
fmt.Println("==> Creating version control...")
}
// if readme flag is set, create readme in project
if CfgGlobal.GetCreateReadmeFlag() {
fmt.Println("==> Creating readme...")
isCreated := CreateReadme(name, description, "BC", "")
if isCreated {
fmt.Println("==> Created README.md")
}
}
}
// if github flag is set, create structure in github
if CfgGlobal.GetCreateGitFlag() {
} else if CfgGlobal.GetCreateTeaFlag() {
err := CreateGiteaProject(data, cmd, args)
if err != nil {
log.Fatal(err)
}
}
return nil
}
// The function creates a directory for a project and changes the current working directory to the
// newly created directory.
func CreateProjectStructure(project structures.Project) {
fmt.Println("==> Creating directory...")
// Create directory of the project
fmt.Println("==> Creating directory...")
utils.CreateDir(project.Name)
fmt.Println("==> Created directory")
// Change to project directory
err := os.Chdir(project.Name)
if err != nil {
log.Fatal(err)
}
}
// The function `CreateGiteaProject` creates a new Gitea project, initializes a Git repository, and
// makes an initial commit.
func CreateGiteaProject(prj structures.Project, cmd *cobra.Command, args []string) error {
// Make API request to create Gitea project
jsonData, _ := json.Marshal(prj)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/repos", CfgGlobal.GetUrlApiOrgs(), CfgGlobal.GetCreateCmd().Organisation),
fmt.Sprintf("%s/%s/repos", CfgGlobal.GetUrlApiOrgs(), CfgGlobal.GetCreateCmd().Organization),
bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "token "+CfgGlobal.GetGitTeaTokenEnv())
req.Header.Set("Content-Type", "application/json")
@@ -89,22 +150,6 @@ func CreateProject(cmd *cobra.Command, args []string) error {
defer res.Body.Close()
// Create directory of the project
fmt.Println("==> Creating directory...")
utils.CreateDir(name)
fmt.Println("==> Created directory")
// Change to project directory
err = os.Chdir(name)
if err != nil {
log.Fatal(err)
}
isCreated := CreateReadme(name, description, "BC", "")
if isCreated {
fmt.Println("==> Created README.md")
}
// Git commands
gitCmd := exec.Command("git", "init")
err = gitCmd.Run()
@@ -144,4 +189,7 @@ func CreateProject(cmd *cobra.Command, args []string) error {
fmt.Println(project)
return nil
}
func CreateGithubProject() {}