Skip to content

Commit

Permalink
Add Cobra and Viper (#3)
Browse files Browse the repository at this point in the history
* add cobra and viper

* add tests for config file

* resolve cyclic imports

* resolve PR's review

* removed Config from versionanalyzer

* rename versionanalyzers field
  • Loading branch information
Adibov authored Nov 7, 2022
1 parent 7be6b55 commit b7a91de
Show file tree
Hide file tree
Showing 25 changed files with 1,055 additions and 236 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
with:
go-version: '1.19.2'
- name: Build
run: go build -o semangit ./src
run: go build -o semangit .
- name: Smoke test
run: ./semangit --help
- uses: actions/upload-artifact@v3
Expand Down
145 changes: 145 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,146 @@
/coverage.txt

# Created by https://www.toptal.com/developers/gitignore/api/goland,go
# Edit at https://www.toptal.com/developers/gitignore?templates=goland,go

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

### Go Patch ###
/vendor/
/Godeps/

### GoLand ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### GoLand Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/

# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/

# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$

# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml

# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml

# End of https://www.toptal.com/developers/gitignore/api/goland,go
64 changes: 64 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"semangit/internal/versionanalyzers/repo"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "run",
Short: "Run semangit",
Long: `A simple tool to force version update in CI.`,
Run: func(cmd *cobra.Command, args []string) {
if err := runSemangit(cmd, args); err != nil {
logrus.WithError(err).Fatal("Failed to run.")
}
},
}

func InitializeFlags() {
rootCmd.Flags().StringP(
"repo-dir",
"r",
".",
"The repository directory",
)
rootCmd.Flags().StringP(
"src-rev",
"s",
"",
"The source git revision",
)
rootCmd.Flags().StringP(
"dest-rev",
"d",
"",
"The destination git revision",
)
rootCmd.Flags().StringP(
"version-analyzer-name",
"n",
"helm",
"The name of the version analyzer you want to use",
)
registerVersionAnalyzersArgumentsFlags(rootCmd)
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func registerVersionAnalyzersArgumentsFlags(cmd *cobra.Command) {
for _, versionAnalyzer := range repo.GetAllAnalyzers() {
argNamePrefix := versionAnalyzer.GetName() + "-"
for _, argDefinition := range versionAnalyzer.GetExtraArgumentDefinitions() {
cmd.Flags().String(argNamePrefix+argDefinition.Name, argDefinition.DefaultValue, argDefinition.Description)
}
}
}
29 changes: 29 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"semangit/internal/config"
"semangit/internal/gitrepo"
"semangit/internal/utils"
"semangit/internal/versionanalyzers/repo"
)

func runSemangit(cmd *cobra.Command, args []string) error {
conf := utils.GetResultOrPanic(config.LoadConfig(cmd))
versionAnalyzer := repo.GetVersionAnalyzer(conf.CurrentVersionAnalyzerName)
repoManager := gitrepo.NewGitRepoManger(conf.RepoDir)

if conf.SrcRevision != gitrepo.RevisionNone {
repoManager.Checkout(conf.SrcRevision)
}

// TODO: Check if r.versionAnalyzer.ChangeNeedsVersionUpdate
srcVersion := utils.GetResultOrPanic(versionAnalyzer.ReadVersion(conf.RepoDir, conf.GetCurrentVersionAnalyzerArgumentValues()))
repoManager.Checkout(conf.DestRevision)
toVersion := utils.GetResultOrPanic(versionAnalyzer.ReadVersion(conf.RepoDir, conf.GetCurrentVersionAnalyzerArgumentValues()))

fmt.Printf("TODO: Compare versions! From Version: %s To Version: %s\n", srcVersion, toVersion)
fmt.Printf("Compare result: %d\n", versionAnalyzer.CompareVersions(srcVersion, toVersion))
return nil
}
31 changes: 26 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@ module semangit

go 1.19

require github.com/go-git/go-git/v5 v5.4.2
require (
github.com/go-git/go-git/v5 v5.4.2
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.4.1
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.13.0
)

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

require (
Expand All @@ -26,9 +47,9 @@ require (
github.com/stretchr/objx v0.4.0 // indirect
github.com/stretchr/testify v1.8.0
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b7a91de

Please sign in to comment.