Skip to content

Commit

Permalink
refactor: package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolnasc committed Dec 24, 2022
1 parent 100bd16 commit 68167a6
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

var RootCmd = &cobra.Command{
Use: "gotcha",
Short: "Pull Request Quality Reviewer",
}

// Starts the root-cli.
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
}
51 changes: 51 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"github.com/diegolnasc/gotcha/pkg/handler"
c "github.com/diegolnasc/gotcha/pkg/model"

g "github.com/diegolnasc/gotcha/pkg/provider/github"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type Provider string

const (
GitHub Provider = "github"
)

// Server command configuration.
type config struct {
providerWorker handler.ProviderWorker
providerName Provider
config c.Settings
}

// Returns the command cli
func Init() *cobra.Command {
c := &config{}
return &cobra.Command{
Use: "server",
Short: "Show the current version",
PreRun: func(cmd *cobra.Command, args []string) {
c.preRun()
},
Run: func(cmd *cobra.Command, args []string) {
c.providerWorker = g.New(&c.config)
h := &handler.ServerHandler{
Config: c.config,
ProviderWorker: c.providerWorker,
}
h.NewServer()
},
}
}

func (c *config) preRun() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./build")
viper.ReadInConfig()
viper.Unmarshal(&c.config)
}
23 changes: 23 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// Version configuration.
type GotchaVersion struct {
Version string
}

// Returns the command cli
func (v *GotchaVersion) Init() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show the current version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Gotcha -> " + v.Version)
},
}
}
12 changes: 12 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

import "testing"

func TestVersion(t *testing.T) {
cmd := &GotchaVersion{Version: "test-1.0.0"}

if err := cmd.Init().Execute(); err != nil {
t.Errorf("unexpected return %s", err.Error())
t.FailNow()
}
}
70 changes: 70 additions & 0 deletions pkg/model/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 Diego Lima. All rights reserved.

// Use of this source code is governed by a Apache license.
// license that can be found in the LICENSE file.

package model

// Settings represents the configuration and authorization aspects.
type Settings struct {
Layout Layout `yaml:"layout"`
Github Github `yaml:"github"`
}

// Layout represents permissions level and pull request functionalities.
type Layout struct {
Administration Administration `yaml:"administration"`
PullRequest PullRequest `yaml:"pullRequest"`
}

// Administration represents general permissions.
type Administration struct {
Permission Permission `yaml:"permission"`
}

// Permission represents high level user's and repository permissions.
type Permission struct {
Users []string `yaml:"users"`
Repositories []Repositories `yaml:"repositories"`
}

// Repositories represents low level permissions in repositories.
type Repositories struct {
Repository Repository `yaml:"repository"`
}

// Repository represents user permission in a single repository.
type Repository struct {
Name string `yaml:"name"`
Users []string `yaml:"users"`
}

// PullRequest represents commands and functionalities.
type PullRequest struct {
EnableOverview bool `yaml:"enableOverview"`
OverViewCommand string `yaml:"overViewCommand"`
ApproveCommand string `yaml:"approveCommand"`
RunTestSuiteCommand string `yaml:"runTestSuiteCommand"`
MergeCommand string `yaml:"mergeCommand"`
MergeAndDeleteCommand string `yaml:"mergeAndDeleteCommand"`
TestSuite TestSuite `yaml:"testSuite"`
}

// TestSuite represents configuration for the test cases.
type TestSuite struct {
NamePattern string `yaml:"namePattern"`
Reviewers bool `yaml:"reviewers"`
Assignees bool `yaml:"assignees"`
Labels bool `yaml:"labels"`
}

// Github represents github app owner configuration.
type Github struct {
AppID int `yaml:"appId"`
Organization string `yaml:"organization"`
User string `yaml:"user"`
WebhookSecret string `yaml:"webhookSecret"`
PrivateKeyLocation string `yaml:"privateKeyLocation"`
PrivateKey string `yaml:"privateKey"`
Events []string `yaml:"events"`
}

0 comments on commit 68167a6

Please sign in to comment.