go-KodeStarter/cmdCreate/createGitea.go

84 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-07-08 00:45:18 -04:00
package cmdCreate
import (
"bytes"
"encoding/json"
"fmt"
"kode-creator/structures"
"log"
"net/http"
"os/exec"
"github.com/spf13/cobra"
)
// 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
}