Skip to content

Commit

Permalink
Lots of changes to IO
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstinnett committed Dec 7, 2021
1 parent 223ab60 commit 86f2622
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
---
linters:
enable:
- godot
- deadcode
- errcheck
- thelper
- gofumpt
- gosimple
- govet
- tparallel
- unconvert
- unparam
- wastedassign
- revive
- predeclared
53 changes: 53 additions & 0 deletions cli/cmd/ci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2021 Drew Stinnett <drew@drewlink.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
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.
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// ciCmd represents the ci command.
var ciCmd = &cobra.Command{
Use: "ci",
Short: "Run CI Pipeline pieces",
Long: `Run CI Pipeline pieces. This will be specific to a git environment, such as GitHub, GitLab, etc`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("ci called")
},
}

func init() {
rootCmd.AddCommand(ciCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// ciCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// ciCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
133 changes: 133 additions & 0 deletions cli/cmd/ciGitlab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright © 2021 Drew Stinnett <drew@drewlink.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
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.
*/
package cmd

import (
"fmt"
"os"
"time"

"github.com/apex/log"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/spf13/cobra"
)

// ciGitlabCmd represents the ciGitlab command.
var ciGitlabCmd = &cobra.Command{
Use: "gitlab",
Short: "Run CI process inside GitLab CI",
Run: func(cmd *cobra.Command, args []string) {
gitDirectory, err := cmd.Flags().GetString("git-directory")
cobra.CheckErr(err)
requiredEnvs := []string{
"GITLAB_USER_EMAIL", "GITLAB_USER_NAME", "GITLAB_TOKEN", "GITLAB_USER_LOGIN", "CI_SERVER_HOST",
"CI_PROJECT_ROOT_NAMESPACE", "CI_PROJECT_NAME", "GITLAB_TOKEN",
}
var missingEnvs []string
for _, requiredEnv := range requiredEnvs {
if _, ok := os.LookupEnv(requiredEnv); !ok {
missingEnvs = append(missingEnvs, requiredEnv)
}
}
if len(missingEnvs) > 0 {
log.Fatalf("Missing required environment variables: %v", missingEnvs)
}

log.Info("Opening Git directory")
r, err := git.PlainOpen(gitDirectory)
cobra.CheckErr(err)

log.Info("Setting Working Directory")
w, err := r.Worktree()
cobra.CheckErr(err)

log.Info("Adding README.md")
_, err = w.Add("README.md")
cobra.CheckErr(err)

log.Info("Getting status")
status, err := w.Status()
cobra.CheckErr(err)

fmt.Println(status)
_, err = w.Commit("chore: labdoc update", &git.CommitOptions{
Author: &object.Signature{
Name: os.Getenv("GITLAB_USER_NAME"),
Email: os.Getenv("GITLAB_USER_EMAIL"),
When: time.Now(),
},
})
cobra.CheckErr(err)

remotes, err := r.Remotes()
cobra.CheckErr(err)
var hasGitlabRemote bool
for _, remote := range remotes {
if remote.Config().Name == "gitlab" {
hasGitlabRemote = true
}
}
if !hasGitlabRemote {
log.Info("Creating remote")
_, err = r.CreateRemote(&config.RemoteConfig{
Name: "gitlab",
URLs: []string{
fmt.Sprintf("https://%v:%v@%v/%v/%v.git",
os.Getenv("GITLAB_USER_LOGIN"),
os.Getenv("GITLAB_TOKEN"),
os.Getenv("CI_SERVER_HOST"),
os.Getenv("CI_PROJECT_ROOT_NAMESPACE"),
os.Getenv("CI_PROJECT_NAME"),
),
},
})
cobra.CheckErr(err)
}

log.Info("Doing push")
err = r.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec("refs/heads/main:refs/heads/main"),
},
})
cobra.CheckErr(err)
},
}

func init() {
ciCmd.AddCommand(ciGitlabCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// ciGitlabCmd.PersistentFlags().String("foo", "", "A help for foo")
ciGitlabCmd.PersistentFlags().StringP("git-directory", "g", ".", "Path to the git directory")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// ciGitlabCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
2 changes: 1 addition & 1 deletion cli/cmd/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/spf13/cobra"
)

// examplesCmd represents the examples command
// examplesCmd represents the examples command.
var examplesCmd = &cobra.Command{
Use: "examples",
Short: "Print plugin examples",
Expand Down
41 changes: 13 additions & 28 deletions cli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,29 @@ THE SOFTWARE.
package cmd

import (
"fmt"
"html/template"
"io/ioutil"
"os"
"strings"

// Register all the template functions
_ "github.com/drewstinnett/labdoc/internal/plugins/all"
"github.com/drewstinnett/labdoc/pkg/labdoc"
// Register all the template functions.
"github.com/apex/log"
labdoctemplate "github.com/drewstinnett/labdoc/pkg/template"
"github.com/spf13/cobra"
)

// generateCmd represents the generate command
// generateCmd represents the generate command.
var generateCmd = &cobra.Command{
Use: "generate template_file",
Short: "Generate README.md",
Short: "Generate a README.md from a template",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateIn, err := ioutil.ReadFile(args[0])
outf, err := cmd.Flags().GetString("out")
cobra.CheckErr(err)

allFunctions := template.FuncMap{}

// Load All plugin data
for name, c := range labdoc.Plugins {
itemFunction, err := c().TemplateFunctions()
cobra.CheckErr(err)
for k, v := range itemFunction {
funcName := fmt.Sprintf("%v%v", name, strings.Title(k))
allFunctions[funcName] = v
}
}

cobra.CheckErr(err)
tpl, err := template.New("tpl").Funcs(allFunctions).Parse(string(templateIn))
cobra.CheckErr(err)
out := os.Stdout
err = tpl.Execute(out, nil)
changed, err := labdoctemplate.Generate(args[0], outf)
cobra.CheckErr(err)
if changed {
log.Infof("Generated new %v", outf)
} else {
log.Infof("No new changes in %v", outf)
}
},
}

Expand All @@ -72,6 +56,7 @@ func init() {
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// generateCmd.PersistentFlags().String("foo", "", "A help for foo")
generateCmd.PersistentFlags().StringP("out", "o", "", "The output file to use")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

var cfgFile string

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "labdoc",
Short: "Generate a personalized README.md for GitLab from a template",
Expand Down
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/drewstinnett/go-output-format v1.0.0
github.com/dustin/go-humanize v1.0.0
github.com/go-git/go-git/v5 v5.4.2
github.com/mitchellh/go-homedir v1.1.0
github.com/mmcdole/gofeed v1.1.3
github.com/spf13/cobra v1.2.1
Expand All @@ -17,30 +18,42 @@ require (
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/andybalholm/cascadia v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
Expand All @@ -49,6 +62,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
Expand Down
Loading

0 comments on commit 86f2622

Please sign in to comment.