mirror of
https://github.com/mustbeperfect/definitive-opensource.git
synced 2026-04-20 12:55:30 +02:00
Populated CLI with initial GO
This commit is contained in:
@@ -1 +1 @@
|
|||||||
The future CLI companion for definitive-opensource.
|
CLI utility. Currently in development.
|
||||||
21
cli/cli.go
Normal file
21
cli/cli.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func Run() error {
|
||||||
|
fmt.Println("Definitive Open Source CLI")
|
||||||
|
fmt.Println("1) Add new project")
|
||||||
|
|
||||||
|
var choice int
|
||||||
|
fmt.Print("Select an option: ")
|
||||||
|
fmt.Scanln(&choice)
|
||||||
|
|
||||||
|
switch choice {
|
||||||
|
case 1:
|
||||||
|
return AddProject()
|
||||||
|
default:
|
||||||
|
fmt.Println("Invalid option")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
50
cli/loaders.go
Normal file
50
cli/loaders.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"definitive-opensource/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadSubcategories() ([]models.Subcategory, error) {
|
||||||
|
file, err := os.ReadFile("categories.json")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var wrapper struct {
|
||||||
|
Subcategories []models.Subcategory `json:"subcategories"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(file, &wrapper)
|
||||||
|
return wrapper.Subcategories, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadPlatforms() ([]models.Platform, error) {
|
||||||
|
file, err := os.ReadFile("platforms.json")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var wrapper struct {
|
||||||
|
Platforms []models.Platform `json:"platforms"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(file, &wrapper)
|
||||||
|
return wrapper.Platforms, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadTags() ([]models.Tag, error) {
|
||||||
|
file, err := os.ReadFile("tags.json")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var wrapper struct {
|
||||||
|
Properties []models.Tag `json:"properties"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(file, &wrapper)
|
||||||
|
return wrapper.Properties, err
|
||||||
|
}
|
||||||
12
cli/main.go
12
cli/main.go
@@ -1,5 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
func main() {
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"definitive-opensource/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := cli.Run(); err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
cli/models.go
Normal file
24
cli/models.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type Subcategory struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Platform struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Application struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
RepoURL string `json:"repo_url"`
|
||||||
|
Tags []string `json:"tags,omitempty"`
|
||||||
|
Platforms []string `json:"platforms,omitempty"`
|
||||||
|
Category string `json:"category"`
|
||||||
|
}
|
||||||
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