Skip to content

Commit

Permalink
feat(args) handle --config-path arg
Browse files Browse the repository at this point in the history
  • Loading branch information
anhgelus committed Mar 29, 2023
1 parent cf74984 commit 8fcbe97
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"embed"
"license-generator/src/args"
"license-generator/src/config"
"license-generator/src/utils"
"os"
"strings"
Expand All @@ -18,6 +19,18 @@ func main() {
if arg.Info {
os.Exit(0)
}
// import custom licenses if needed
if arg.ConfigPath != "" {
licenses, err := config.GetLicenseConfigs(arg.ConfigPath)
utils.HandleError(err)
contextPath, err := os.Getwd()
utils.HandleError(err)
println(contextPath)
for _, license := range licenses {
println("Path in main.go:", license.Path)
}
config.AddLicensesToMap(licenses, contextPath)
}
arg.HandleArgs()

l := findLicense(arg.LicenseType)
Expand Down
3 changes: 0 additions & 3 deletions resources/example/config/cc0.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
customLicenses = []

[CC0]
path="./cc0.license"
name="Creative Commons 0"
identifier="cc0"
2 changes: 1 addition & 1 deletion resources/example/config/config.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
customLicenses = []
customLicenses = ["cc0"]
10 changes: 9 additions & 1 deletion src/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Arguments struct {
LicenseType License
Year string
Authors []string
ConfigPath string
Question bool
Info bool
}
Expand Down Expand Up @@ -89,12 +90,17 @@ var (
Description: "Set the authors of the project, separate them with the coma (,)",
Argument: "[string,]",
}
ConfigPath = AvailableArgument{
Parameter: "config-path",
Description: "Set a path to the config for using custom licenses",
Argument: "string",
}
HelpArg = InfoArgument{
Parameter: "h",
textGenerator: helpText,
Description: "Show the help",
}
argLists = [4]AvailableArgument{AppNameArg, LicenseArg, YearArg, AuthorsArg}
argLists = [5]AvailableArgument{AppNameArg, LicenseArg, YearArg, AuthorsArg, ConfigPath}
infoArgLists = [0]InfoArgument{}
)

Expand Down Expand Up @@ -126,6 +132,8 @@ func (arg *Arguments) assignValueToArguments(argument *AvailableArgument, v stri
arg.Year = v
case "authors":
arg.Authors = parseAuthors(v)
case "config-path":
arg.ConfigPath = v
default:
return errors.New("unknown argument, use -h to see every arguments")
}
Expand Down
10 changes: 5 additions & 5 deletions src/args/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (arg *Arguments) HandleArgs() {
}
m := make(map[string]string)
m["App Name"] = arg.AppName
m["License"] = string(arg.LicenseType)
m["License"] = arg.LicenseType.Name
m["Author(s)"] = utils.StringArrayToString(arg.Authors)
m["Year(s)"] = arg.Year
utils.GenerateSumeUp("Options", m, "-")
Expand All @@ -74,17 +74,17 @@ func (arg *Arguments) handleQuestion() {
println("The name is: " + name)
arg.AppName = name
}
if arg.LicenseType == "" {
if arg.LicenseType.Name == "" {
oldLicense := ""
print("License: ")
err := utils.Scan(&oldLicense)
utils.HandleError(err)
license := GetLicense(oldLicense)
if license == "" {
license, found := GetLicense(oldLicense)
if !found {
println("Unknown license type. Aborted.")
os.Exit(2)
}
println("The license is: " + license)
println("The license is: " + license.Name)
arg.LicenseType = license
}
if len(arg.Authors) == 0 {
Expand Down
25 changes: 19 additions & 6 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,44 @@ func GetLicenseConfigs(path string) ([]LicenseConfig, error) {
content := utils.FileContent(path, file)
if file.Name() == "config.toml" {
utils.DecodeToml(content, &config)
println("Found the config.toml file")
continue
}
var license LicenseConfig
utils.DecodeToml(content, &license)
licenses[license.Identifier] = license
println("Imported", license.Name)
}
final := make([]LicenseConfig, len(licenses))
if len(config.ListConfig) == 0 {
for _, s := range config.ListConfig {
v, found := licenses[s]
if !found {
return nil, errors.New("the license with the identifier " + s + " was not found")
}
final = append(final, v)
i := 0
for _, licenseConfig := range licenses {
final[i] = licenseConfig
i++
}
return final, nil
}
i := 0
for _, s := range config.ListConfig {
v, found := licenses[s]
if !found {
return nil, errors.New("the license with the identifier " + s + " was not found")
}
final[i] = v
i++
}
return final, nil
}

func (license *LicenseConfig) AddToMap(contextPath string) {
println("Path:", license.Path)
args.AddLicense(license.ToLicense(contextPath), license.Identifier)
}

func AddLicensesToMap(licenses []LicenseConfig, contextPath string) {
println("F:", licenses[0].Path)
for _, license := range licenses {
println("P:", license.Path)
license.AddToMap(contextPath)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func DecodeToml(content []byte, t any) {
//
// contextPath: the contextual path, with a slash (/) at the end
func RelativeToAbsolute(path string, contextPath string) string {
println(path, contextPath)
switch string(path[0]) {
case "/":
return path
Expand Down

0 comments on commit 8fcbe97

Please sign in to comment.