go-KodeStarter/cmdCreate/create.go

196 lines
4.6 KiB
Go
Raw Normal View History

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-07-01 00:49:45 -04:00
// The CreateProject function creates a project structure, including a directory, version control, and
// a readme file, based on the provided arguments and flags.
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)
2023-07-01 00:49:45 -04:00
// 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)
2023-06-17 14:17:02 -04:00
req, err := http.NewRequest("POST",
2023-07-01 00:49:45 -04:00
fmt.Sprintf("%s/%s/repos", CfgGlobal.GetUrlApiOrgs(), CfgGlobal.GetCreateCmd().Organization),
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()
// 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
2023-07-01 00:49:45 -04:00
2023-06-17 14:17:02 -04:00
}
2023-07-01 00:49:45 -04:00
func CreateGithubProject() {}