Skip to content

Commit

Permalink
as far as im getting tonight
Browse files Browse the repository at this point in the history
  • Loading branch information
migsc committed Jan 9, 2025
1 parent c823446 commit 6be0c86
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 314 deletions.
32 changes: 26 additions & 6 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,35 @@ func copyIncludedFile(includedFilePath string, targetDir string) error {
return fmt.Errorf("could not expand the path: %s\n%v", includedFilePath, err)
}

// We can't do this here because it would result in files being removed iteratively.
// if err := file.SetupEmptyDir(targetDir); err != nil {
// return err
// }
// Get the source file's info to check permissions
fileInfo, err := os.Stat(expandedPath)
if err != nil {
return fmt.Errorf("could not stat file: %s\n%v", expandedPath, err)
}

cmd := exec.Command("cp", "-r", expandedPath, targetDir)
// Use cp with -p flag to preserve mode, ownership, timestamps
cmd := exec.Command("cp", "-pr", expandedPath, targetDir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()

// If the file is executable for user, group, or others, log it
if fileInfo.Mode()&0111 != 0 {
cmd2 := exec.Command("chmod", "+x", filepath.Join(targetDir, filepath.Base(expandedPath)))
cmd2.Stdout = os.Stdout
cmd2.Stderr = os.Stderr
log.Info("copying executable file",
"file", includedFilePath,
"mode", fileInfo.Mode().String(),
)
err = cmd.Run()
if err != nil {
return err
}
return cmd2.Run() // TODO: This is not working. Could we move it to the binary directory and then run it from there?
} else {
return cmd.Run()
}

}

// type FileManifest struct {
Expand Down
219 changes: 116 additions & 103 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var samplePY []byte
//go:embed init/greet.sh
var sampleSH []byte

//go:embed init/greet.go
var sampleGO []byte

type TemplateVariable = struct {
Name string
Value *string
Expand All @@ -70,14 +73,14 @@ var templateVariables map[string]TemplateVariable = map[string]TemplateVariable{
}

// resetTemplateVariables resets all template variables to their default values
func resetTemplateVariables() {
cliName = ""
cliDescription = "A CLI tool"
cliVersion = "0.0.1"
cliAuthor = "Unknown"
cliLicense = "MIT"
cliLanguage = "javascript"
}
// func resetTemplateVariables() {
// cliName = ""
// cliDescription = "A CLI tool"
// cliVersion = "0.0.1"
// cliAuthor = "Unknown"
// cliLicense = "MIT"
// cliLanguage = "javascript"
// }

// initCmd represents the init command
var initCmd = &cobra.Command{
Expand All @@ -88,7 +91,7 @@ var initCmd = &cobra.Command{

RunE: func(cmd *cobra.Command, args []string) error {
// Reset template variables at the start of each run
resetTemplateVariables()
// resetTemplateVariables()

// Determine name of the project
log.Info("Initializing cmdeagle project")
Expand Down Expand Up @@ -121,111 +124,121 @@ var initCmd = &cobra.Command{

// Only show interactive form if we're not in a test environment
if os.Getenv("GO_TEST") != "1" {
form := huh.NewForm(
// Gather some final details about the order.
huh.NewGroup(
huh.NewInput().
Title("What’s your CLI's name?.").
Description("This will be the name of the binary and the directory.").
// TODO implement rename command then change to: Description("This will be the name of the binary and the directory. You can change this later with `cmdeagle rename` command. It must be lowercase.").
Placeholder(templateVariables["name"].Placeholder).
Value(templateVariables["name"].Value).
Validate(func(str string) error {
if str == "" {
return fmt.Errorf("cli name cannot be empty")
}

if !regexp.MustCompile(`^[a-z0-9\_\-]+$`).MatchString(str) {
return fmt.Errorf("cli name must be only lowercase letters(a-z), numbers(0-9), hypens( - ), and underscores( _ ) are allowed")
}

return nil
}),
),
huh.NewGroup(
huh.NewInput().
Title("Description (optional).").
Description("This will be the description of the binary and the directory.").
Placeholder(templateVariables["description"].Placeholder).
Value(templateVariables["description"].Value),
huh.NewInput().
Title("Version (optional).").
Placeholder(templateVariables["version"].Placeholder).
Value(templateVariables["version"].Value),
huh.NewInput().
Title("Author (optional).").
Placeholder(templateVariables["author"].Placeholder).
Value(templateVariables["author"].Value),
huh.NewInput().
Title("License (optional).").
Placeholder(templateVariables["license"].Placeholder).
Value(templateVariables["license"].Value),
).Description("Some optional metadata to document your CLI. Displayed by the help command."),
// huh.NewGroup(
// huh.NewSelect[string]().
// Title("Choose a language/runtime to generate sample code for? (optional).").
// Description("We'll generate sample code showing how to integrate languages/runtimes you choose.").
// Options(
// // huh.NewOption("go", "Go"),
// huh.NewOption("python", "Python"),
// // huh.NewOption("rust", "Rust"),
// huh.NewOption("javascript", "JavaScript"),
// // huh.NewOption("typescript", "TypeScript"),
// ).
// Value(&cliLanguages),
// ),
)

err = form.Run()
form := huh.NewForm(
// Gather some final details about the order.
huh.NewGroup(
huh.NewInput().
Title("What’s your CLI's name?.").
Description("This will be the name of the binary and the directory.").
// TODO implement rename command then change to: Description("This will be the name of the binary and the directory. You can change this later with `cmdeagle rename` command. It must be lowercase.").
Placeholder(templateVariables["name"].Placeholder).
Value(templateVariables["name"].Value).
Validate(func(str string) error {
if str == "" {
return fmt.Errorf("cli name cannot be empty")
}

if !regexp.MustCompile(`^[a-z0-9\_\-]+$`).MatchString(str) {
return fmt.Errorf("cli name must be only lowercase letters(a-z), numbers(0-9), hypens( - ), and underscores( _ ) are allowed")
}

return nil
}),
),
huh.NewGroup(
huh.NewInput().
Title("Description (optional).").
Description("This will be the description of the binary and the directory.").
Placeholder(templateVariables["description"].Placeholder).
Value(templateVariables["description"].Value),
huh.NewInput().
Title("Version (optional).").
Placeholder(templateVariables["version"].Placeholder).
Value(templateVariables["version"].Value),
huh.NewInput().
Title("Author (optional).").
Placeholder(templateVariables["author"].Placeholder).
Value(templateVariables["author"].Value),
huh.NewInput().
Title("License (optional).").
Placeholder(templateVariables["license"].Placeholder).
Value(templateVariables["license"].Value),
).Description("Some optional metadata to document your CLI. Displayed by the help command."),
// huh.NewGroup(
// huh.NewSelect[string]().
// Title("Choose a language/runtime to generate sample code for? (optional).").
// Description("We'll generate sample code showing how to integrate languages/runtimes you choose.").
// Options(
// // huh.NewOption("go", "Go"),
// huh.NewOption("python", "Python"),
// // huh.NewOption("rust", "Rust"),
// huh.NewOption("javascript", "JavaScript"),
// // huh.NewOption("typescript", "TypeScript"),
// ).
// Value(&cliLanguages),
// ),
)

err = form.Run()

if err != nil {
log.Fatal(err)
}
}

// Create the .cmd.yaml file
fmt.Printf("Creating .cmd.yaml for %s\n", cliName)
_, err = os.Create(".cmd.yaml")
if err != nil {
log.Fatal(err)
fmt.Printf("Error creating .cmd.yaml: %v\n", err)
return err
}
}

// Create the .cmd.yaml file
fmt.Printf("Creating .cmd.yaml for %s\n", cliName)
_, err := os.Create(".cmd.yaml")
if err != nil {
fmt.Printf("Error creating .cmd.yaml: %v\n", err)
return err
}
// Replace template variables
interpolatedYAML := string(sampleYAMLConfig)
for name, variable := range templateVariables {
interpolatedYAML = strings.ReplaceAll(interpolatedYAML, templateVariablePrefix+name+templateVariableSuffix, *variable.Value)
}

// Replace template variables
interpolatedYAML := string(sampleYAMLConfig)
for name, variable := range templateVariables {
interpolatedYAML = strings.ReplaceAll(interpolatedYAML, templateVariablePrefix+name+templateVariableSuffix, *variable.Value)
}
// Write the file
err = os.WriteFile(".cmd.yaml", []byte(interpolatedYAML), 0644)
if err != nil {
log.Error("Failed to write .cmd.yaml", "error", err)
return fmt.Errorf("failed to write .cmd.yaml: %w", err)
}

// Write the file
err = os.WriteFile(".cmd.yaml", []byte(interpolatedYAML), 0644)
if err != nil {
log.Error("Failed to write .cmd.yaml", "error", err)
return fmt.Errorf("failed to write .cmd.yaml: %w", err)
}
// Create the greet.sh file
err = os.WriteFile("greet.sh", sampleSH, 0644)
if err != nil {
log.Error("Failed to write greet.sh", "error", err)
return fmt.Errorf("failed to write greet.sh: %w", err)
}

// Create the greet.sh file
err = os.WriteFile("greet.sh", sampleSH, 0644)
if err != nil {
log.Error("Failed to write greet.sh", "error", err)
return fmt.Errorf("failed to write greet.sh: %w", err)
}
// Create the greet.js file
err = os.WriteFile("greet.js", sampleJS, 0644)
if err != nil {
log.Error("Failed to write greet.js", "error", err)
return fmt.Errorf("failed to write greet.js: %w", err)
}

// Create the greet.js file
err = os.WriteFile("greet.js", sampleJS, 0644)
if err != nil {
log.Error("Failed to write greet.js", "error", err)
return fmt.Errorf("failed to write greet.js: %w", err)
}
// Create the greet.py file
err = os.WriteFile("greet.py", samplePY, 0644)
if err != nil {
log.Error("Failed to write greet.py", "error", err)
return fmt.Errorf("failed to write greet.py: %w", err)
}

// Create the greet.go file
err = os.WriteFile("greet.go", sampleGO, 0644)
if err != nil {
log.Error("Failed to write greet.go", "error", err)
return fmt.Errorf("failed to write greet.go: %w", err)
}

// Create the greet.py file
err = os.WriteFile("greet.py", samplePY, 0644)
if err != nil {
log.Error("Failed to write greet.py", "error", err)
return fmt.Errorf("failed to write greet.py: %w", err)
log.Info("Successfully initialized cmdeagle project", "name", cliName)
return nil
}

log.Info("Successfully initialized cmdeagle project", "name", cliName)
return nil
},
}
44 changes: 44 additions & 0 deletions cmd/init/greet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
"os"
"strconv"
"strings"
)

func main() {
// Get arguments from environment variables
name := getEnv("ARGS_NAME")
age := getEnv("ARGS_AGE")

// Get flags from environment variables
uppercase := getEnv("FLAGS_UPPERCASE") == "true"
lowercase := getEnv("FLAGS_LOWERCASE") == "true"
repeat, _ := strconv.Atoi(getEnv("FLAGS_REPEAT"))

// Construct base greeting
greeting := fmt.Sprintf("Hello %s!", name)
if age != "" {
greeting += fmt.Sprintf(" You are %s years old.", age)
}

// Apply case transformations
if uppercase {
greeting = strings.ToUpper(greeting)
} else if lowercase {
greeting = strings.ToLower(greeting)
}

// Output greeting with repetition
for i := 0; i < repeat; i++ {
fmt.Println(greeting)
}
}

func getEnv(key string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return ""
}
Loading

0 comments on commit 6be0c86

Please sign in to comment.