mirror of
https://github.com/mustbeperfect/definitive-opensource.git
synced 2026-04-18 12:54:09 +02:00
Populated CLI with initial GO
This commit is contained in:
48
cli/new/add.go
Normal file
48
cli/new/add.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"definitive-opensource/data"
|
||||
"definitive-opensource/models"
|
||||
)
|
||||
|
||||
func AddProject() error {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
fmt.Print("GitHub repo URL: ")
|
||||
scanner.Scan()
|
||||
repo := strings.TrimSpace(scanner.Text())
|
||||
|
||||
fmt.Print("Project name: ")
|
||||
scanner.Scan()
|
||||
name := strings.TrimSpace(scanner.Text())
|
||||
|
||||
category, err := selectCategory(scanner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
platforms, err := selectPlatforms(scanner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tags, err := selectTags(scanner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entry := models.Application{
|
||||
Name: name,
|
||||
RepoURL: repo,
|
||||
Category: category,
|
||||
Platforms: platforms,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
return data.AppendApplication(entry)
|
||||
}
|
||||
29
cli/new/append.go
Normal file
29
cli/new/append.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"definitive-opensource/models"
|
||||
)
|
||||
|
||||
func AppendApplication(app models.Application) error {
|
||||
file, err := os.ReadFile("applications.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var apps []models.Application
|
||||
if err := json.Unmarshal(file, &apps); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apps = append(apps, app)
|
||||
|
||||
out, err := json.MarshalIndent(apps, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile("applications.json", out, 0644)
|
||||
}
|
||||
42
cli/new/select_category.go
Normal file
42
cli/new/select_category.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"definitive-opensource/data"
|
||||
)
|
||||
|
||||
func selectCategory(scanner *bufio.Scanner) (string, error) {
|
||||
subs, err := data.LoadSubcategories()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for {
|
||||
fmt.Print("Search category: ")
|
||||
scanner.Scan()
|
||||
query := strings.ToLower(scanner.Text())
|
||||
|
||||
filtered := []string{}
|
||||
for _, s := range subs {
|
||||
if strings.Contains(strings.ToLower(s.Name), query) {
|
||||
filtered = append(filtered, fmt.Sprintf("%s (%s)", s.Name, s.ID))
|
||||
}
|
||||
}
|
||||
|
||||
for i, f := range filtered {
|
||||
fmt.Printf("%d) %s\n", i+1, f)
|
||||
}
|
||||
|
||||
fmt.Print("Select category number: ")
|
||||
var idx int
|
||||
fmt.Scanln(&idx)
|
||||
if idx > 0 && idx <= len(filtered) {
|
||||
return subs[idx-1].ID, nil
|
||||
}
|
||||
|
||||
fmt.Println("Invalid selection, try again")
|
||||
}
|
||||
}
|
||||
35
cli/new/select_platforms.go
Normal file
35
cli/new/select_platforms.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
|
||||
"definitive-opensource/data"
|
||||
)
|
||||
|
||||
func selectPlatforms(scanner *bufio.Scanner) ([]string, error) {
|
||||
platforms, err := data.LoadPlatforms()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
selected := []string{}
|
||||
|
||||
for {
|
||||
fmt.Println("Select platforms (0 to finish):")
|
||||
for i, p := range platforms {
|
||||
fmt.Printf("%d) %s\n", i+1, p.Name)
|
||||
}
|
||||
|
||||
var idx int
|
||||
fmt.Scanln(&idx)
|
||||
if idx == 0 {
|
||||
break
|
||||
}
|
||||
if idx > 0 && idx <= len(platforms) {
|
||||
selected = append(selected, platforms[idx-1].ID)
|
||||
}
|
||||
}
|
||||
|
||||
return selected, nil
|
||||
}
|
||||
35
cli/new/select_tags.go
Normal file
35
cli/new/select_tags.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
|
||||
"definitive-opensource/data"
|
||||
)
|
||||
|
||||
func selectTags(scanner *bufio.Scanner) ([]string, error) {
|
||||
tags, err := data.LoadTags()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
selected := []string{}
|
||||
|
||||
for {
|
||||
fmt.Println("Select tags (0 to finish):")
|
||||
for i, t := range tags {
|
||||
fmt.Printf("%d) %s\n", i+1, t.Name)
|
||||
}
|
||||
|
||||
var idx int
|
||||
fmt.Scanln(&idx)
|
||||
if idx == 0 {
|
||||
break
|
||||
}
|
||||
if idx > 0 && idx <= len(tags) {
|
||||
selected = append(selected, tags[idx-1].ID)
|
||||
}
|
||||
}
|
||||
|
||||
return selected, nil
|
||||
}
|
||||
Reference in New Issue
Block a user