-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from edwinvautier/feat_install-command
Feat install command
- Loading branch information
Showing
14 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
|
||
go get -u github.com/edwinvautier/go-cli | ||
go mod tidy |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
/* | ||
Copyright © 2021 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import ( | ||
"github.com/edwinvautier/go-cli/services/installCommand" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// installCmd represents the install command | ||
var installCmd = &cobra.Command{ | ||
Use: "install", | ||
Short: "Install bundles to your app", | ||
Long: `A command that install bundles from edwinvautier/go-cli/bundles`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
for _, bundleName := range args { | ||
if err := installCommand.InstallBundle(bundleName); err != nil { | ||
log.Error(err) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(installCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,4 +94,4 @@ func createConfig(homeDir string) { | |
log.Error("Couldn't create config file : ", err) | ||
} | ||
initConfig() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,5 +70,5 @@ type CreateCmdConfig struct { | |
Args []string | ||
Box *packr.Box | ||
ProjectPath string | ||
AuthModule bool | ||
AuthModule bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
"os" | ||
) | ||
|
||
// InitInstallCmdConfig creates the needed config for the create command by prompting user and doing other actions | ||
func InitInstallCmdConfig(config *InstallCmdConfig) error { | ||
workdir, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
viper.AddConfigPath(workdir) | ||
viper.SetConfigName(".go-cli-config") | ||
if err := viper.ReadInConfig(); err != nil { | ||
return err | ||
} | ||
|
||
config.GoPackageFullPath = viper.GetString("package") | ||
|
||
return nil | ||
} | ||
|
||
// UpdateConfigAfterInstalling set the new bundle to true in config after install | ||
func UpdateConfigAfterInstalling(name string) error { | ||
workdir, err := os.Getwd() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
viper.AddConfigPath(workdir) | ||
viper.SetConfigName(".go-cli-config") | ||
viper.Set("bundles."+name, true) | ||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
log.Info("Using config file : ", viper.ConfigFileUsed()) | ||
viper.WriteConfig() | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
// InstallCmdConfig is the struct for the templates config of install command | ||
type InstallCmdConfig struct { | ||
GoPackageFullPath string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package installCommand | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/edwinvautier/go-cli/config" | ||
"github.com/gobuffalo/packr/v2" | ||
) | ||
|
||
// InstallBundle install bundle from its name | ||
func InstallBundle(name string) error { | ||
var installCmdConfig config.InstallCmdConfig | ||
config.InitInstallCmdConfig(&installCmdConfig) | ||
box := packr.New("Bundles", "../../bundles") | ||
|
||
if !bundleExists(name, box) { | ||
return errors.New(name + " bundle does not exist") | ||
} | ||
|
||
// execute templates | ||
if err := executeTemplates(name, installCmdConfig); err != nil { | ||
return err | ||
} | ||
|
||
// if it exists, execute the shell script it contains | ||
if err := executeInstallScript(box, name); err != nil { | ||
return err | ||
} | ||
|
||
// load & update config | ||
return config.UpdateConfigAfterInstalling(name) | ||
} | ||
|
||
func bundleExists(name string, box *packr.Box) bool { | ||
files := box.List() | ||
|
||
for _, file := range files { | ||
bundleName := strings.Split(file, "/")[0] | ||
if bundleName == name { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package installCommand | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/edwinvautier/go-cli/services/filesystem" | ||
"github.com/gobuffalo/packr/v2" | ||
) | ||
|
||
func executeInstallScript(box *packr.Box, name string) error { | ||
workdir, err := os.Getwd() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fileString, err := box.FindString("/" + name + "/install.sh") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ioutil.WriteFile(workdir+"/install.sh", []byte(fileString), 0755) | ||
exec.Command("sh", "install.sh").Run() | ||
|
||
return filesystem.RemoveSingle(workdir + "/install.sh") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package installCommand | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/edwinvautier/go-cli/config" | ||
"github.com/edwinvautier/go-cli/helpers" | ||
"github.com/gobuffalo/packd" | ||
"github.com/gobuffalo/packr/v2" | ||
log "github.com/sirupsen/logrus" | ||
"text/template" | ||
) | ||
|
||
func executeTemplates(name string, installCmdConfig config.InstallCmdConfig) error { | ||
box := packr.New("templates", "../../bundles/"+name+"/templates") | ||
|
||
box.Walk(func(path string, f packd.File) error { | ||
fInfo, _ := f.FileInfo() | ||
fileParts := helpers.GetFilePartsFromName(fInfo.Name()) | ||
generateFile(fileParts.Path, fileParts.Name, fileParts.OutputName, installCmdConfig, box) | ||
return nil | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func generateFile(path string, name string, outputName string, installCmdConfig config.InstallCmdConfig, box *packr.Box) { | ||
// Get template content as string | ||
templateString, err := box.FindString(path + name) | ||
if err != nil { | ||
log.Error(err) | ||
return | ||
} | ||
workdir, err := os.Getwd() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
// Create the directory if not exist | ||
if _, err := os.Stat(workdir + "/" + path); os.IsNotExist(err) { | ||
os.MkdirAll(workdir+"/"+path, os.ModePerm) | ||
} | ||
|
||
err = executeTemplate(installCmdConfig, outputName, workdir+"/"+path, templateString) | ||
if err != nil { | ||
log.Error(err) | ||
return | ||
} | ||
} | ||
|
||
func executeTemplate(installCmdConfig config.InstallCmdConfig, outputName string, path string, templateString string) error { | ||
// Create the file | ||
file, err := os.Create(path + outputName) | ||
if err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
// Execute template and write file | ||
parsedTemplate := template.Must(template.New("template").Parse(templateString)) | ||
err = parsedTemplate.Execute(file, installCmdConfig) | ||
|
||
return nil | ||
} |