Skip to content

Commit

Permalink
Begin implementing mantil cli
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Jul 7, 2021
1 parent 60c805f commit 9d888ea
Show file tree
Hide file tree
Showing 11 changed files with 923 additions and 47 deletions.
115 changes: 115 additions & 0 deletions cmd/mantil/cmd/init.go
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)
}
65 changes: 65 additions & 0 deletions cmd/mantil/cmd/root.go
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())
}
}
4 changes: 3 additions & 1 deletion cmd/mantil/main.go
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()
}
7 changes: 4 additions & 3 deletions cmd/mantil/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"
"testing"

"github.com/atoz-technology/mantil-cli/pkg/mantil"
"github.com/stretchr/testify/require"
)

Expand All @@ -23,8 +24,8 @@ func setDevEnvPaths(t *testing.T) {
func TestExample(t *testing.T) {
setDevEnvPaths(t)

spa := &Spa{}
spa.testData()
err := spa.Apply()
p := &mantil.Project{}
p.TestData()
err := p.Apply()
require.NoError(t, err)
}
Binary file removed cmd/mantil/mantil
Binary file not shown.
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ module github.com/atoz-technology/mantil-cli

go 1.16

require github.com/stretchr/testify v1.7.0 // indirect
require (
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v36 v36.0.0 // indirect
github.com/manifoldco/promptui v0.8.0
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
)
Loading

0 comments on commit 9d888ea

Please sign in to comment.