Skip to content

Commit

Permalink
cmd updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvzhz committed Nov 28, 2024
1 parent 1ca11d6 commit 4ad33c1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 55 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
1 change: 1 addition & 0 deletions akita/cmd/builderTemplate.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Builder can create the component {{packageName}} with different configurations.
package {{packageName}}

import "github.com/sarchlab/akita/v4/sim"
Expand Down
1 change: 1 addition & 0 deletions akita/cmd/compTemplate.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Comp provides the component {{packageName}} with the ticking functionality.
package {{packageName}}

import "github.com/sarchlab/akita/v4/sim"
Expand Down
33 changes: 16 additions & 17 deletions akita/cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ var builderTemplate string
//go:embed compTemplate.txt
var compTemplate string

// componentCmd represents the main component command
var componentCmd = &cobra.Command{
Use: "component",
Short: "Create and manage components.",
Long: "`component --create [ComponentName]` creates a new component.",
Long: "`component --create [ComponentName]` creates a new component folder containing the builder and comp files",
Run: func(cmd *cobra.Command, args []string) {
componentName, _ := cmd.Flags().GetString("create") // fetch the string value of the flag
componentName, _ := cmd.Flags().GetString("create")
if componentName != "" {
if !inGitRepo() {
log.Fatalf("Error: This command must be run inside a Git repository.")
Expand All @@ -37,15 +36,15 @@ var componentCmd = &cobra.Command{

errFile := generateBuilderFile(componentName)
if errFile != nil {
fmt.Printf("Error generating builder file: %v\n", errFile)
} else {
log.Fatalf("Failed to generate builder file for component '%s': %v\n", componentName, errFile)
} else {
fmt.Println("Builder File generated successfully!")
}

errComp := generateCompFile(componentName)
if errComp != nil {
fmt.Printf("Error generating comp file: %v\n", errComp)
} else {
log.Fatalf("Failed to generate comp file for component '%s': %v\n", componentName, errFile)
} else {
fmt.Println("Comp File generated successfully!")
}

Expand All @@ -57,7 +56,7 @@ var componentCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(componentCmd)
componentCmd.Flags().String("create", "", "Create a new component")
componentCmd.Flags().String("create", "", "create a new component")
}

// Check if current operation is in a Git repository
Expand All @@ -66,7 +65,7 @@ func inGitRepo() bool {
cmd.Dir = filepath.Dir(".")
output, err := cmd.Output()
if err != nil {
log.Printf("Error running git command: %v\n", err)
log.Fatalf("Error running git command: %v\n", err)
return false
}
return strings.TrimSpace(string(output)) == "true"
Expand All @@ -82,15 +81,15 @@ func createComponentFolder(name string) error {
// Create basic files for the new component
func generateBuilderFile(folder string) error {
// Ensure the folder exists before proceeding
folder = filepath.Join("./akita", folder)
_, errFind := os.Stat(folder)
folderPath := filepath.Join("./akita", folder)
_, errFind := os.Stat(folderPath)
if os.IsNotExist(errFind) {
return fmt.Errorf("failed to find folder: %s", folder)
return fmt.Errorf("failed to find folder: %s", folderPath)
} else if errFind != nil {
return fmt.Errorf("error checking folder: %v", errFind)
}

filePath := filepath.Join(folder, "builder.go")
filePath := filepath.Join(folderPath, "builder.go")
placeholder := "{{packageName}}"
packageName := folder
content := strings.Replace(builderTemplate, placeholder, packageName, -1)
Expand All @@ -105,15 +104,15 @@ func generateBuilderFile(folder string) error {

func generateCompFile(folder string) error {
// Ensure the folder exists before proceeding
folder = filepath.Join("./akita", folder)
_, errFind := os.Stat(folder)
folderPath := filepath.Join("./akita", folder)
_, errFind := os.Stat(folderPath)
if os.IsNotExist(errFind) {
return fmt.Errorf("failed to find folder: %s", folder)
return fmt.Errorf("failed to find folder: %s", folderPath)
} else if errFind != nil {
return fmt.Errorf("error checking folder: %v", errFind)
}

filePath := filepath.Join(folder, "comp.go")
filePath := filepath.Join(folderPath, "comp.go")
placeholder := "{{packageName}}"
packageName := folder
content := strings.Replace(compTemplate, placeholder, packageName, -1)
Expand Down
33 changes: 7 additions & 26 deletions akita/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,23 @@ import (
"github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "akita",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Short: "akita command",
Long: `akita command is the root command for Akita framework.
It contains child commands that help users to manage components and files
in Akita.`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.akita.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


func main() {

Check failure on line 27 in akita/cmd/root.go

View workflow job for this annotation

GitHub Actions / Akita Compilation

func `main` is unused (unused)

Check failure on line 27 in akita/cmd/root.go

View workflow job for this annotation

GitHub Actions / Akita Compilation

func `main` is unused (unused)
Execute()
}
11 changes: 0 additions & 11 deletions main.go

This file was deleted.

0 comments on commit 4ad33c1

Please sign in to comment.