Skip to content

Commit

Permalink
refactoring ee cli to use providers
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Jun 5, 2024
1 parent c0d097e commit 042245f
Show file tree
Hide file tree
Showing 18 changed files with 663 additions and 1,275 deletions.
3 changes: 2 additions & 1 deletion cli/cmd/digger/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
core_backend "github.com/diggerhq/digger/cli/pkg/core/backend"
core_policy "github.com/diggerhq/digger/cli/pkg/core/policy"
"github.com/diggerhq/digger/cli/pkg/usage"
"github.com/diggerhq/digger/libs/comment_utils/reporting"
core_locking "github.com/diggerhq/digger/libs/locking"
"github.com/diggerhq/digger/libs/orchestrator"
Expand All @@ -28,7 +29,7 @@ var applyCmd = &cobra.Command{

prService, orgService, reporter, err := runConfig.GetServices()
if err != nil {
reportErrorAndExit(runConfig.Actor, "Unrecognised reporter: "+runConfig.Reporter, 1)
usage.ReportErrorAndExit(runConfig.Actor, "Unrecognised reporter: "+runConfig.Reporter, 1)
}

apply(runConfig.Actor, args[0], runConfig.RepoNamespace, runConfig.PRNumber, lock, PolicyChecker, *reporter, *prService, *orgService, BackendApi)
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/diggerhq/digger/cli/pkg/azure"
"github.com/diggerhq/digger/cli/pkg/digger"
"github.com/diggerhq/digger/cli/pkg/usage"
comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary"
"github.com/spf13/cobra"
"log"
"os"
Expand All @@ -20,7 +21,7 @@ var defaultCmd = &cobra.Command{
switch ci {
case digger.GitHub:
logLeader = os.Getenv("GITHUB_ACTOR")
gitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy)
gitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderBasic{})
case digger.GitLab:
logLeader = os.Getenv("CI_PROJECT_NAME")
gitLabCI(lock, PolicyChecker, BackendApi, ReportStrategy)
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/digger/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
core_backend "github.com/diggerhq/digger/cli/pkg/core/backend"
core_policy "github.com/diggerhq/digger/cli/pkg/core/policy"
"github.com/diggerhq/digger/cli/pkg/usage"
"github.com/diggerhq/digger/libs/comment_utils/reporting"
core_locking "github.com/diggerhq/digger/libs/locking"
"github.com/diggerhq/digger/libs/orchestrator"
Expand All @@ -28,7 +29,7 @@ var destroyCmd = &cobra.Command{

prService, orgService, reporter, err := runConfig.GetServices()
if err != nil {
reportErrorAndExit(runConfig.Actor, "Unrecognised reporter: "+runConfig.Reporter, 1)
usage.ReportErrorAndExit(runConfig.Actor, "Unrecognised reporter: "+runConfig.Reporter, 1)
}

destroy(runConfig.Actor, args[0], runConfig.RepoNamespace, runConfig.PRNumber, lock, PolicyChecker, *reporter, *prService, *orgService, BackendApi)
Expand Down
221 changes: 83 additions & 138 deletions cli/cmd/digger/main.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cli/cmd/digger/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
core_backend "github.com/diggerhq/digger/cli/pkg/core/backend"
core_policy "github.com/diggerhq/digger/cli/pkg/core/policy"
"github.com/diggerhq/digger/cli/pkg/usage"
"github.com/diggerhq/digger/libs/comment_utils/reporting"
core_locking "github.com/diggerhq/digger/libs/locking"
"github.com/diggerhq/digger/libs/orchestrator"
Expand All @@ -28,7 +29,7 @@ var planCmd = &cobra.Command{

prService, orgService, reporter, err := runConfig.GetServices()
if err != nil {
reportErrorAndExit(runConfig.Actor, "Unrecognised reporter: "+runConfig.Reporter, 1)
usage.ReportErrorAndExit(runConfig.Actor, "Unrecognised reporter: "+runConfig.Reporter, 1)
}

plan(runConfig.Actor, args[0], runConfig.RepoNamespace, runConfig.PRNumber, lock, PolicyChecker, *reporter, *prService, *orgService, BackendApi)
Expand Down
36 changes: 2 additions & 34 deletions cli/cmd/digger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func PreRun(cmd *cobra.Command, args []string) {
hostName := os.Getenv("DIGGER_HOSTNAME")
token := os.Getenv("DIGGER_TOKEN")
orgName := os.Getenv("DIGGER_ORGANISATION")
BackendApi = NewBackendApi(hostName, token)
PolicyChecker = NewPolicyChecker(hostName, orgName, token)
BackendApi = backend.NewBackendApi(hostName, token)
PolicyChecker = policy.NewPolicyChecker(hostName, orgName, token)

if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" {
ReportStrategy = &reporting.CommentPerRunStrategy{
Expand Down Expand Up @@ -118,38 +118,6 @@ func PreRun(cmd *cobra.Command, args []string) {
log.Println("Lock provider has been created successfully")
}

func NewBackendApi(hostName string, authToken string) core_backend.Api {
var backendApi core_backend.Api
if os.Getenv("NO_BACKEND") == "true" {
log.Println("WARNING: running in 'backendless' mode. Features that require backend will not be available.")
backendApi = backend.NoopApi{}
} else {
backendApi = backend.DiggerApi{
DiggerHost: hostName,
AuthToken: authToken,
HttpClient: http.DefaultClient,
}
}
return backendApi
}

func NewPolicyChecker(hostname string, organisationName string, authToken string) core_policy.Checker {
var policyChecker core_policy.Checker
if os.Getenv("NO_BACKEND") == "true" {
log.Println("WARNING: running in 'backendless' mode. Features that require backend will not be available.")
policyChecker = policy.NoOpPolicyChecker{}
} else {
policyChecker = policy.DiggerPolicyChecker{
PolicyProvider: &policy.DiggerHttpPolicyProvider{
DiggerHost: hostname,
DiggerOrganisation: organisationName,
AuthToken: authToken,
HttpClient: http.DefaultClient,
}}
}
return policyChecker
}

var rootCmd = &cobra.Command{
Use: "digger",
Short: "An open source IaC orchestration tool",
Expand Down
17 changes: 17 additions & 0 deletions cli/pkg/backend/diggerapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/diggerhq/digger/cli/pkg/core/backend"
"github.com/diggerhq/digger/cli/pkg/core/execution"
"github.com/diggerhq/digger/libs/orchestrator/scheduler"
"github.com/diggerhq/digger/libs/terraform_utils"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
Expand Down Expand Up @@ -185,3 +187,18 @@ func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId

return &response, nil
}

func NewBackendApi(hostName string, authToken string) backend.Api {
var backendApi backend.Api
if os.Getenv("NO_BACKEND") == "true" {
log.Println("WARNING: running in 'backendless' mode. Features that require backend will not be available.")
backendApi = NoopApi{}
} else {
backendApi = DiggerApi{
DiggerHost: hostName,
AuthToken: authToken,
HttpClient: http.DefaultClient,
}
}
return backendApi
}
Loading

0 comments on commit 042245f

Please sign in to comment.