change linux

This commit is contained in:
bcharest 2023-07-08 00:45:18 -04:00
parent 71fe930228
commit 7e2e5c85f5
10 changed files with 252 additions and 125 deletions

View File

@ -1,16 +1,11 @@
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"
@ -27,7 +22,7 @@ func InitConfig(config *config.Config) {
var CreateCmd = &cobra.Command{
Use: "create",
Short: "Create Github project",
Short: "CREATE Github project",
Long: `A simple CLI app to create a startup project on Github`,
Run: func(cmd *cobra.Command, args []string) {
@ -79,6 +74,8 @@ func CreateProject(cmd *cobra.Command, args []string) error {
}
}
// if readme flag is set, create readme in project
if CfgGlobal.GetCreateReadmeFlag() {
fmt.Println("==> Creating readme...")
@ -87,10 +84,9 @@ func CreateProject(cmd *cobra.Command, args []string) error {
fmt.Println("==> Created README.md")
}
}
}
// if github flag is set, create structure in github
if CfgGlobal.GetCreateGitFlag() {
if CfgGlobal.GetCreateTeaFlag() {
} else if CfgGlobal.GetCreateTeaFlag() {
@ -104,92 +100,3 @@ func CreateProject(cmd *cobra.Command, args []string) error {
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() {}

26
cmdCreate/createDir.go Normal file
View File

@ -0,0 +1,26 @@
package cmdCreate
import (
"fmt"
"kode-creator/structures"
"kode-creator/utils"
"log"
"os"
)
// 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)
}
}

83
cmdCreate/createGitea.go Normal file
View File

@ -0,0 +1,83 @@
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
}

View File

@ -0,0 +1,5 @@
package cmdCreate
func CreateGithubProject() {
}

View File

@ -24,7 +24,7 @@ func InitConfig(config *config.Config) {
var EnvCmd = &cobra.Command{
Use: "env",
Short: "manage environment variables",
Short: "Manage ENVIRONMENT variables",
Long: `A simple CLI option to manage environment variables`,
Run: func(cmd *cobra.Command, args []string) {

View File

@ -18,7 +18,7 @@ func ListGitHubOrganization() ([]string, error) {
func ListGitHubProjectsfromUser(username string, token string) ([]*github.Repository, error) {
// Replace with your access token
// token := "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"
//token = "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"
// Create an oauth2 token source with the access token
tokenSource := oauth2.StaticTokenSource(
@ -40,3 +40,42 @@ func ListGitHubProjectsfromUser(username string, token string) ([]*github.Reposi
return repos, nil
}
// function that return a list of github projects base on a search string
func SearchGitHubProjectsfromSearch(search string, token string) ([]*github.Repository, error) {
// Create client
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client := github.NewClient(tc)
// Search repositories
results, _, err := client.Search.Repositories(context.Background(), search, &github.SearchOptions{})
if err != nil {
return nil, err
}
// Return list of repositories
var repos []*github.Repository
// for _, repo := range results.Repositories {
// repos = append(repos, &repo)
// }
fmt.Printf("Got %d results from GitHub\n", len(results.Repositories))
for _, repo := range results.Repositories {
fmt.Printf("%s %s\n", *repo.Name, *repo.HTMLURL)
repos = append(repos, &repo)
}
fmt.Printf("Appended %d repos to slice\n", len(repos))
for i, repo := range repos {
fmt.Printf("%d %s %s\n", i, *repo.Name, *repo.HTMLURL)
}
fmt.Println("Number of repos:", len(repos))
fmt.Println("Number of results:", *results.Total)
return repos, nil
}

View File

@ -26,7 +26,7 @@ func InitConfig(config *config.Config) {
// for a specified organization or lists all organizations, depending on the command line arguments.
var ListCmd = &cobra.Command{
Use: "list",
Short: "List Github, Gitea orgs, project",
Short: "LIST Github, Gitea orgs, project",
Long: `A simple CLI app to list Github, Gitea orgs, project`,
Run: func(cmd *cobra.Command, args []string) {
@ -49,6 +49,7 @@ func startList(cmd *cobra.Command, args []string) {
utils.PrintHeader()
}
// if Gitea flag is set
if CfgGlobal.GetTeaFlag() {
// if -o flag is set, list projects for that org
@ -86,7 +87,13 @@ func startList(cmd *cobra.Command, args []string) {
}
fmt.Println(utils.CreateLine(8))
}
// if Github flag is set
} else if CfgGlobal.GetGitFlag() {
// if user flag is set, list projects for that user
if CfgGlobal.GetListUserFlag() {
fmt.Println("\n L I S T G I T H U B P R O J E C T S F O R : " + CfgGlobal.GetListUser())
fmt.Println(utils.CreateLine(11))
repos, err := ListGitHubProjectsfromUser(CfgGlobal.GetListUser(), CfgGlobal.GetGithubTokenEnv())
@ -99,7 +106,24 @@ func startList(cmd *cobra.Command, args []string) {
utils.PrintTreeElement(nb, *repo.Name, *repo.HTMLURL)
nb++
}
} else if CfgGlobal.GetGitHubSearchFlag() {
fmt.Println("\n L I S T G I T H U B P R O J E C T S F R O M S E A R C H : " + CfgGlobal.GetListCmd().GitHubSearch)
fmt.Println(utils.CreateLine(11))
repos, err := SearchGitHubProjectsfromSearch(CfgGlobal.GetListCmd().GitHubSearch, CfgGlobal.GetGithubTokenEnv())
if err != nil {
fmt.Println("Error:", err)
return
}
nb := 1
for _, repo := range repos {
utils.PrintTreeElement(nb+1, *repo.Name, *repo.HTMLURL)
// nb++
}
}
// for the rest of the cases print help
} else {
// print help if no flag is set

View File

@ -39,7 +39,10 @@ type ListCmdConfig struct {
GitFlag bool
TeaFlag bool
User string
UserFlag bool
listOrganizationFlag bool
GitHubSearchFlag bool
GitHubSearch string
}
type CreateCmdConfig struct {
@ -295,6 +298,36 @@ func (c *Config) SetListUser(value string) {
c.ListCmd.User = value
}
// getter for ListUserFlag
func (c *Config) GetListUserFlag() bool {
return c.ListCmd.UserFlag
}
// setter for ListUserFlag
func (c *Config) SetListUserFlag(value bool) {
c.ListCmd.UserFlag = value
}
// getter for GitHubSearchFlag
func (c *Config) GetGitHubSearchFlag() bool {
return c.ListCmd.GitHubSearchFlag
}
// setter for GitHubSearchFlag
func (c *Config) SetGitHubSearchFlag(value bool) {
c.ListCmd.GitHubSearchFlag = value
}
// getter for GitHubSearch
func (c *Config) GetGitHubSearch() string {
return c.ListCmd.GitHubSearch
}
// setter for GitHubSearch
func (c *Config) SetGitHubSearch(value string) {
c.ListCmd.GitHubSearch = value
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * C R E A T E C M D GETTER - SETTER * * * *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -537,7 +570,7 @@ func (c *Config) Init() {
// set Gitea token from environment variable
var giteaTokenEnv string = utils.GetEnvVarValueFromOsEnv("GITEA_TOKEN")
c.SetGithubTokenEnv(giteaTokenEnv)
c.SetGitTeaTokenEnv(giteaTokenEnv)
// set verbose flag
c.SetVerboseFlag(false)
@ -618,6 +651,7 @@ func (c *Config) InitCmdList(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("user") || cmd.Flags().Changed("u") {
c.SetListUser(cmd.Flag("user").Value.String())
c.SetListUserFlag(true)
}
if cmd.Flags().Changed("git") || cmd.Flags().Changed("g") {
@ -628,6 +662,11 @@ func (c *Config) InitCmdList(cmd *cobra.Command, args []string) {
c.SetTeaFlag(true)
}
if cmd.Flags().Changed("search") || cmd.Flags().Changed("s") {
c.SetGitHubSearchFlag(true)
c.SetGitHubSearch(cmd.Flag("search").Value.String())
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -32,19 +32,21 @@ func init() {
// root menu section
// - - - - - - - - - - -
rootCmd.Flags().BoolP("help", "h", false, "Show help for "+CfgGlobal.GetApplicationName())
rootCmd.Flags().BoolVarP(&CfgGlobal.VersionFlag, "version", "V", false, "Show VERSION of "+CfgGlobal.GetApplicationName())
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().BoolVarP(&CfgGlobal.VerboseFlag, "verbose", "v", false, "VERBOSE mode output")
rootCmd.PersistentFlags().BoolVarP(&CfgGlobal.JsonFlag, "json", "j", false, "JSON output format")
rootCmd.PersistentFlags().BoolVarP(&CfgGlobal.VerboseFlag, "verbose", "v", false, "VERBOSE mode output")
rootCmd.Flags().BoolVarP(&CfgGlobal.VersionFlag, "version", "V", false, "Show VERSION of "+CfgGlobal.GetApplicationName())
rootCmd.Flags().BoolP("help", "h", false, "Show help for "+CfgGlobal.GetApplicationName())
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.Flags().SortFlags = false
// List menu section
// - - - - - - - - - - -
cmdLists.ListCmd.Flags().StringVarP(&CfgGlobal.TokenEnv, "token", "t", "", "TOKEN for Gitea or Github")
cmdLists.ListCmd.Flags().BoolVarP(&CfgGlobal.ListCmd.GitFlag, "git", "g", false, "List all repositories from GITHUB")
cmdLists.ListCmd.Flags().BoolVarP(&CfgGlobal.ListCmd.TeaFlag, "tea", "a", false, "List all repositories from GITEA")
cmdLists.ListCmd.Flags().StringVarP(&CfgGlobal.ListCmd.User, "search", "s", "", "search for request on GITHUB or GITEA")
cmdLists.ListCmd.Flags().StringVarP(&CfgGlobal.ListCmd.User, "user", "u", "", "user for request on GITHUB or GITEA")
cmdLists.ListCmd.Flags().StringVarP(&CfgGlobal.ListCmd.Organization, "org", "o", "", "List projects from an ORGANIZATION")
cmdLists.ListCmd.Flags().SortFlags = false
// Create menu section (create project directories structure, init git, Github or Gitea repository, create README.md template )
// - - - - - - - - - - - - - - - - - - -
@ -61,6 +63,7 @@ func init() {
cmdCreate.CreateCmd.Flags().StringVarP(&CfgGlobal.TokenEnv, "token", "t", "", "TOKEN for Gitea or Github")
cmdCreate.CreateCmd.Flags().StringVarP(&CfgGlobal.CreateCmd.Organization, "org", "o", "", "ORGANIZATION for this project")
cmdCreate.CreateCmd.Flags().BoolVarP(&CfgGlobal.CreateCmd.PrivateFlag, "private", "p", false, "PRIVATE with this switch")
cmdCreate.CreateCmd.Flags().SortFlags = false
// Env menu section
// - - - - - - - - - - -
@ -71,6 +74,7 @@ func init() {
cmdEnv.EnvCmd.Flags().StringVarP(&CfgGlobal.EnvCmd.EnvVariablesCategory, "cat", "c", "", "FILTER for a specific category")
cmdEnv.EnvCmd.Flags().BoolVarP(&CfgGlobal.EnvCmd.EnvVariablesSetFlag, "set", "s", false, "Set environnement variable available from config.yml")
cmdEnv.EnvCmd.Flags().BoolVarP(&CfgGlobal.EnvCmd.EnvVariablesShowAllFlag, "all", "a", false, "Show all environnement variables")
cmdEnv.EnvCmd.Flags().SortFlags = false
// Add subcommands
rootCmd.AddCommand(cmdLists.ListCmd)
@ -112,7 +116,7 @@ func main() {
cmdEnv.InitConfig(CfgGlobal)
// set debug mode
CfgGlobal.SetDebugMode(false)
CfgGlobal.SetDebugMode(true)
CfgGlobal.DebugPrintConfig("Function main")
// execute root command

View File

@ -15,7 +15,7 @@ type Version struct {
// version variable that will be set at build time by versionManager.exe
// *** DO NOT EDIT ***
var versionNumber Version = Version{0, 0, 0, 41}
var versionNumber Version = Version{0, 0, 0, 42}
// GetFullVersion returns the full version number as a string
func GetFullVersion() string {