init project
This commit is contained in:
commit
05d83c52c3
4 changed files with 116 additions and 0 deletions
28
cmd/cli/main.go
Normal file
28
cmd/cli/main.go
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.juancwu.dev/juancwu/forgejo-cli/internal/cli"
|
||||||
|
"git.juancwu.dev/juancwu/forgejo-cli/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var configPath string
|
||||||
|
flag.StringVar(&configPath, "config", "", "Configuration file path")
|
||||||
|
|
||||||
|
cfg, err := config.Load(configPath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
model := cli.New(cfg)
|
||||||
|
err = model.Run()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module git.juancwu.dev/juancwu/forgejo-cli
|
||||||
|
|
||||||
|
go 1.25.6
|
||||||
13
internal/cli/cli.go
Normal file
13
internal/cli/cli.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import "git.juancwu.dev/juancwu/forgejo-cli/internal/config"
|
||||||
|
|
||||||
|
type Model struct{}
|
||||||
|
|
||||||
|
func New(cfg *config.Config) *Model {
|
||||||
|
return &Model{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) Run() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
72
internal/config/config.go
Normal file
72
internal/config/config.go
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
ForgejoInstanceURL string `json:"forgejo_instance_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load tries to load a saved configuration.
|
||||||
|
func Load(path string) (*Config, error) {
|
||||||
|
var err error
|
||||||
|
if path == "" {
|
||||||
|
path, err = getUserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to load config: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
return &Config{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fileData, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg Config
|
||||||
|
err = json.Unmarshal(fileData, &cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) Save(path string) error {
|
||||||
|
var err error
|
||||||
|
if path == "" {
|
||||||
|
path, err = getUserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to save config: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileData, err := json.MarshalIndent(cfg, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(path, fileData, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getUserConfigDir creates the full path to the config file.
|
||||||
|
func getUserConfigDir() (string, error) {
|
||||||
|
userConfigPath, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to get user configuration path: %w", err)
|
||||||
|
}
|
||||||
|
filename := filepath.Join(userConfigPath, "forgejo-cli", "config.json")
|
||||||
|
return filename, nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue