package cmdCreate import ( "bytes" "encoding/json" "fmt" "kode-creator/config" "kode-creator/structures" "kode-creator/utils" "log" "net/http" "os" "os/exec" "github.com/joho/godotenv" "github.com/spf13/cobra" ) var token, org, name, private, description string // initailize global config var CfgGlobal *config.Config // Declare a global variable for config func InitConfig(config *config.Config) { CfgGlobal = config // Assign the passed config to the global variable } var CreateCmd = &cobra.Command{ Use: "create", Short: "Create Github project", Long: `A simple CLI app to create a startup project on Github`, Run: func(cmd *cobra.Command, args []string) { err := CreateProject(cmd, args) if err != nil { fmt.Println("Error:", err) return } }, } // 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() CfgGlobal.DebugPrintConfig("Function CreateProject") // initialize global config for command list CfgGlobal.InitCmdCreate(cmd, args) // print header if json flag is not set if !CfgGlobal.GetJsonFlag() { utils.PrintHeader() } // Construct POST data data := structures.Project{ Name: name, Description: description, Private: private == "true", } data.Name = CfgGlobal.GetCreateName() data.Description = CfgGlobal.GetCreateDescription() fmt.Println("Structure Name:" + data.Name) // 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().Organization), bytes.NewBuffer(jsonData)) req.Header.Set("Authorization", "token "+CfgGlobal.GetGitTeaTokenEnv()) req.Header.Set("Content-Type", "application/json") client := &http.Client{} res, err := client.Do(req) if err != nil { log.Fatalln(err) } var dataReceived structures.GitOrgsRepoResponse err = json.NewDecoder(res.Body).Decode(&dataReceived) if err != nil { log.Fatal(err) } fmt.Println(dataReceived) fmt.Printf("==> Created project '%s' URL: '%s'\n", name, dataReceived.CloneURL) defer res.Body.Close() // Git commands gitCmd := exec.Command("git", "init") err = gitCmd.Run() if err != nil { log.Fatal(err) } else { fmt.Printf("==> Initialized empty Git repository in %s\n", name) } gitCmd = exec.Command("git", "checkout", "-b", "main") err = gitCmd.Run() if err != nil { log.Fatal(err) } else { fmt.Println("==> Switched to a new branch 'main'") } gitCmd = exec.Command("git", "add", "-A") err = gitCmd.Run() if err != nil { log.Fatal(err) } else { fmt.Println("==> Switched to a new branch 'main'") } gitCmd = exec.Command("git", "commit", "-m", "first commit from project creator !") err = gitCmd.Run() if err != nil { log.Fatal(err) } else { fmt.Println("==> first commit from Kode-Creator !") } // Get project info from API response var project structures.Project json.NewDecoder(res.Body).Decode(&project) fmt.Println(project) return nil } func CreateGithubProject() {}