Skip to content

Commit

Permalink
refactor: 📝 rename config "file" to config "source"
Browse files Browse the repository at this point in the history
  • Loading branch information
OnCloud125252 committed Apr 21, 2024
1 parent 18c500a commit b44112d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
33 changes: 0 additions & 33 deletions commands/config.go

This file was deleted.

6 changes: 3 additions & 3 deletions commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func Install(c *cli.Context) {
}

module.CreateCacheDotFolder()
config, err := module.GetConfigs()
config, err := module.GetSource()
if err != nil {
log.Fatal(err)
}

packageName := c.Args().Get(0)
if packageName != "" {
for _, file := range config.Files {
for _, file := range config.Source {
if file.Name == packageName {
filePath := module.DownloadFileToCache(file.URL)
installPackage(filePath)
Expand All @@ -37,7 +37,7 @@ func Install(c *cli.Context) {
log.Fatal("Package not found")
}

for _, file := range config.Files {
for _, file := range config.Source {
filePath := module.DownloadFileToCache(file.URL)
installPackage(filePath)
}
Expand Down
37 changes: 37 additions & 0 deletions commands/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package commands

import (
"log"
"ui/cli/module"

"github.com/gookit/color"
"github.com/urfave/cli/v2"
)

func Source(c *cli.Context) {
if (c.String("list") == "") || (c.String("name") == "") || (c.String("url") == "") {
config, err := module.GetSource()
if err != nil {
log.Fatal(err)
}

if len(config.Source) == 0 {
color.Yellowln("No files are installed.")
return
}

for _, file := range config.Source {
color.Cyanf("%s: ", file.Name)
color.Greenln(file.URL)
}

return
}

config, err := module.SetSource(c.String("name"), c.String("url"))
if err != nil {
log.Fatal(err)
}

module.SaveToConfigFile(config)
}
24 changes: 12 additions & 12 deletions module/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"path/filepath"
)

type FileLink struct {
type SourceObject struct {
Name string `json:"name"`
URL string `json:"url"`
}

type ConfigFileJSON struct {
Files []FileLink `json:"files"`
type SourceJSON struct {
Source []SourceObject `json:"source"`
}

func CreateConfigFile() {
Expand Down Expand Up @@ -43,8 +43,8 @@ func CreateConfigFile() {
}
}

func GetConfigs() (ConfigFileJSON, error) {
var config ConfigFileJSON
func GetSource() (SourceJSON, error) {
var config SourceJSON

home, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -72,20 +72,20 @@ func GetConfigs() (ConfigFileJSON, error) {
return config, nil
}

func SetFileURLConfigs(name string, URL string) (ConfigFileJSON, error) {
config, err := GetConfigs()
func SetSource(name string, URL string) (SourceJSON, error) {
config, err := GetSource()
if err != nil {
return config, err
}

for i, file := range config.Files {
for i, file := range config.Source {
if file.Name == name {
fmt.Printf("The URL of the name is already exist. Would you like to change the URL of %s? (y/n) ", name)
fmt.Printf("The URL of the name is already exist. Would you like to change the URL of %s? (y/N) ", name)
var answer string
fmt.Scanln(&answer)

if answer == "y" || answer == "Y" {
config.Files[i].URL = URL
config.Source[i].URL = URL
return config, nil
} else {
fmt.Println("No changes were made")
Expand All @@ -95,12 +95,12 @@ func SetFileURLConfigs(name string, URL string) (ConfigFileJSON, error) {
}

// append new file
config.Files = append(config.Files, FileLink{Name: name, URL: URL})
config.Source = append(config.Source, SourceObject{Name: name, URL: URL})

return config, nil
}

func SaveToConfigFile(config ConfigFileJSON) error {
func SaveToConfigFile(config SourceJSON) error {
home, err := os.UserHomeDir()
if err != nil {
return err
Expand Down

0 comments on commit b44112d

Please sign in to comment.