2023-06-21 11:50:12 -04:00
|
|
|
package cmdCreate
|
2023-06-12 22:40:22 -04:00
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-30 15:25:39 -04:00
|
|
|
"kode-creator/config"
|
|
|
|
"kode-creator/structures"
|
|
|
|
"kode-creator/utils"
|
2023-06-17 14:17:02 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var token, org, name, private, description string
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
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) {
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
err := CreateProject(cmd, args)
|
2023-06-17 14:17:02 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
func CreateProject(cmd *cobra.Command, args []string) error {
|
2023-06-17 14:17:02 -04:00
|
|
|
|
|
|
|
godotenv.Load()
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
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()
|
|
|
|
}
|
2023-06-17 14:17:02 -04:00
|
|
|
|
|
|
|
// Construct POST data
|
|
|
|
data := structures.Project{
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
Private: private == "true",
|
|
|
|
}
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
data.Name = CfgGlobal.GetCreateName()
|
|
|
|
data.Description = CfgGlobal.GetCreateDescription()
|
2023-06-17 14:17:02 -04:00
|
|
|
|
|
|
|
fmt.Println("Structure Name:" + data.Name)
|
|
|
|
|
|
|
|
// Make API request to create Github project
|
|
|
|
jsonData, _ := json.Marshal(data)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST",
|
2023-06-30 10:09:01 -04:00
|
|
|
fmt.Sprintf("%s/%s/repos", CfgGlobal.GetUrlApiOrgs(), CfgGlobal.GetCreateCmd().Organisation),
|
2023-06-17 14:17:02 -04:00
|
|
|
bytes.NewBuffer(jsonData))
|
2023-06-30 15:25:39 -04:00
|
|
|
req.Header.Set("Authorization", "token "+CfgGlobal.GetGitTeaTokenEnv())
|
2023-06-17 14:17:02 -04:00
|
|
|
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()
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
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
|
|
|
|
}
|