go run ./ i to init conf file

This commit is contained in:
bel
2025-04-05 01:03:19 -06:00
parent 984b53c6f1
commit 12bb9c808b

31
main.go
View File

@@ -7,6 +7,7 @@ import (
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"os/signal"
@@ -19,6 +20,15 @@ import (
yaml "gopkg.in/yaml.v3"
)
type Yaml struct {
C Fields
O string
D bool
P []string
}
const YamlFile = ".show-ingestion.yaml"
type Fields struct {
Title string
Season string
@@ -34,28 +44,33 @@ func main() {
foo := Main
if len(os.Args) == 2 && os.Args[1] == "r" {
foo = Recursive
} else if len(os.Args) == 2 && os.Args[1] == "i" {
foo = Stage
}
if err := foo(ctx); err != nil {
panic(err)
}
}
func Stage(ctx context.Context) error {
if _, err := os.Stat(YamlFile); err == nil {
return nil
}
b, _ := yaml.Marshal(Yaml{D: true})
return ioutil.WriteFile(YamlFile, b, os.ModePerm)
}
func Recursive(ctx context.Context) error {
q := []string{"./"}
for len(q) > 0 {
d := q[0]
q = q[1:]
p := path.Join(d, ".show-ingestion.yaml")
p := path.Join(d, YamlFile)
if _, err := os.Stat(p); err != nil {
} else if err := func() error {
var y struct {
C Fields
O string
D bool
P []string
}
b, _ := os.ReadFile(path.Join(d, ".show-ingestion.yaml"))
var y Yaml
b, _ := os.ReadFile(path.Join(d, YamlFile))
if err := yaml.Unmarshal(b, &y); err != nil {
return fmt.Errorf("%s: %w", p, err)
}