-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
923 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/atoz-technology/mantil-cli/pkg/mantil" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/config" | ||
"github.com/go-git/go-git/v5/plumbing/transport/http" | ||
"github.com/google/go-github/github" | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
// initCmd represents the init command | ||
var initCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "Initializes a mantil project", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
namePrompt := promptui.Prompt{ | ||
Label: "Project name", | ||
Default: "test", | ||
} | ||
projectName, err := namePrompt.Run() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
project := mantil.NewProject(projectName) | ||
// TODO check project name availability | ||
// cfg, err := config.LoadDefaultConfig(context.TODO()) | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
// client := s3.NewFromConfig(cfg) | ||
// _, err = client.HeadBucket(context.TODO(), &s3.HeadBucketInput{ | ||
// Bucket: aws.String(project.Bucket), | ||
// }) | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
templateUrl := "https://github.com/atoz-technology/go-mantil-template.git" | ||
_, err = git.PlainClone(project.Name, false, &git.CloneOptions{ | ||
URL: templateUrl, | ||
Progress: os.Stdout, | ||
Depth: 1, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = os.RemoveAll(fmt.Sprintf("%s/.git", project.Name)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
repo, err := git.PlainInit(project.Name, false) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
wt, err := repo.Worktree() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = wt.AddGlob(".") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
_, err = wt.Commit("initial commit", &git.CommitOptions{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
githubAuthToken, ok := os.LookupEnv("GITHUB_TOKEN") | ||
if !ok { | ||
log.Fatal("GITHUB_TOKEN not found in env") | ||
} | ||
githubClient := github.NewClient( | ||
oauth2.NewClient( | ||
context.Background(), | ||
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubAuthToken}), | ||
), | ||
) | ||
private := true | ||
githubRepo, _, err := githubClient.Repositories.Create(context.Background(), "", &github.Repository{ | ||
Name: &project.Name, | ||
Private: &private, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
remoteName := "origin" | ||
remote, err := repo.CreateRemote(&config.RemoteConfig{ | ||
Name: remoteName, | ||
URLs: []string{*githubRepo.HTMLURL}, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = remote.Push(&git.PushOptions{ | ||
RemoteName: remoteName, | ||
Auth: &http.BasicAuth{ | ||
Username: "mantil", | ||
Password: githubAuthToken, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(initCmd) | ||
} |
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,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "mantil", | ||
Short: "A brief description of your application", | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// 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() { | ||
cobra.CheckErr(rootCmd.Execute()) | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// 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/.mantil.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") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
// Search config in home directory with name ".mantil" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(".mantil") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package main | ||
|
||
func main() { | ||
import "github.com/atoz-technology/mantil-cli/cmd/mantil/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
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
Binary file not shown.
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
Oops, something went wrong.