Skip to content

Commit

Permalink
start - adding start/stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
prawn185 committed Jan 30, 2024
0 parents commit b6337ce
Show file tree
Hide file tree
Showing 9 changed files with 530 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
/.env
/.idea
105 changes: 105 additions & 0 deletions cmd/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"bufio"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
)

// Mmm structs
// I actually lvoe this part of go
type Repository struct {
FullName string `json:"full_name"`
CloneURL string `json:"clone_url"`
}

var cloneCmd = &cobra.Command{
Use: "clone",
Short: "Gets all git repos, tries to figure out which one you wanna download, and then downloads it",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: clone,
}

func init() {
godotenv.Load()
rootCmd.AddCommand(cloneCmd)
}

func clone(cmd *cobra.Command, args []string) {
fmt.Println("clone called")
//we'll do this one in indivudual funcs
//connect to GH
//get all repos
repos := connect()
//ask user which one they want

fmt.Println("Select a project to start:")
for i, repo := range repos {
fmt.Printf("[%d] %s\n", i+1, repo.FullName)
}

scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Enter your choice: ")
scanner.Scan()
choice := scanner.Text()

choiceIndex, err := strconv.Atoi(choice)
if err != nil || choiceIndex < 1 || choiceIndex > len(repos) {
fmt.Println("Invalid choice")
return
}

//download it

}

func connect() []Repository {

// i'll need to restructure this if I make a github app, can't seem to get pricate repos working

client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/user/repos", nil)
if err != nil {
log.Fatal(err)
}

req.Header.Add("Authorization", "token "+os.Getenv("GITHUB_ACCESS_TOKEN"))

response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()

responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}

var repositories []Repository
err = json.Unmarshal(responseData, &repositories)
if err != nil {
log.Fatal(err)
}

for _, repo := range repositories {
fmt.Println(repo.FullName)
fmt.Println(repo.CloneURL)
}
return repositories
}
67 changes: 67 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "Lists available projects",
Long: `Lists available projects. For example:`,
Run: availableProjects,
}

func init() {
rootCmd.AddCommand(listCmd)
}

/*
This was my old method to get all the projects, my new one is now in start.go fun! :)
*/
func availableProjects(cmd *cobra.Command, args []string) {
path := "../"
file1 := "docker-compose.yml"
file2 := "makefile"
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
prefix := "project-"
directories, err := ioutil.ReadDir(path)
if err != nil {
fmt.Printf("Error reading directory: %s\n", err)
return
}

for _, dir := range directories {
if dir.IsDir() && strings.HasPrefix(dir.Name(), prefix) {
dirPath := filepath.Join(path, dir.Name())
files, err := ioutil.ReadDir(dirPath)
if err != nil {
fmt.Printf("Error reading directory '%s': %s\n", dirPath, err)
continue
}

found1, found2 := false, false
for _, file := range files {
if file.Name() == file1 {
found1 = true
} else if file.Name() == file2 {
found2 = true
}
}

if found1 || found2 {
fmt.Println(green("✔"), dir.Name())
} else {
fmt.Println(red("✖"), dir.Name())
}

}
}
}
45 changes: 45 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "llama-orc",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,

//Run: func(cmd *cobra.Command, args []string) {},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.llama-orc.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
Loading

0 comments on commit b6337ce

Please sign in to comment.