Skip to content

Commit

Permalink
feat(spctl): Spec writer
Browse files Browse the repository at this point in the history
  • Loading branch information
koltyakov committed Apr 16, 2023
1 parent e256f5a commit f5876f0
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/spctl/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var stratsConf = map[string]Strategy{
Creds: credsResolver.ntlm,
},
"saml": {
Desc: "SAML (Client Credentials) [SPO]",
Desc: "SAML (User Credentials) [SPO]",
Docs: "https://go.spflow.com/auth/strategies/saml",
Envs: []string{SPO},
Creds: credsResolver.saml,
Expand Down
63 changes: 55 additions & 8 deletions cmd/spctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/koltyakov/gosip/api"
)

var pluginVersion = "v1.6.2"

func main() {
siteURL := getSiteURL()
strategy := getStrategy(siteURL)
Expand All @@ -22,6 +24,40 @@ func main() {
fmt.Printf("\033[31mError: %s\033[0m\n", err)
return
}

version, _ := getPluginVersion()
source := getSourceName()
destination := getDestination()

spec := &SourceSpec{
Name: source,
Registry: "github",
Path: "koltyakov/sharepoint",
Version: version,
Destinations: []string{destination},
Spec: PluginSpec{
Auth: AuthSpec{
Strategy: strategy,
Creds: append([][]string{{"siteUrl", siteURL}}, creds...),
},
},
}

if err := spec.Save(source + ".yml"); err != nil {
fmt.Printf("\033[31mError: %s\033[0m\n", err)
return
}
}

func action[T any](message string, fn func() (T, error)) (T, error) {
fmt.Printf("\033[33m%s\033[0m", message)
data, err := fn()
if err != nil {
fmt.Print("\033[2K\r")
return data, err
}
fmt.Print("\033[2K\r")
return data, nil
}

func getSiteURL() string {
Expand Down Expand Up @@ -96,13 +132,24 @@ func checkAuth(siteURL, strategy string, creds [][]string) (*api.SP, error) {
return sp, nil
}

func action[T any](message string, fn func() (T, error)) (T, error) {
fmt.Printf("\033[33m%s\033[0m", message)
data, err := fn()
if err != nil {
fmt.Print("\033[2K\r")
return data, err
func getSourceName() string {
var sourceName string
sourceNameQ := &survey.Input{
Message: "Source name:",
Default: "sharepoint",
Help: "Source name to be used in the config file",
}
fmt.Print("\033[2K\r")
return data, nil
survey.AskOne(sourceNameQ, &sourceName, survey.WithValidator(survey.Required))
return sourceName
}

func getDestination() string {
var destination string
destinationNameQ := &survey.Input{
Message: "Destination name:",
Default: "postgres",
Help: "Destination name to be used in the config file",
}
survey.AskOne(destinationNameQ, &destination, survey.WithValidator(survey.Required))
return destination
}
32 changes: 32 additions & 0 deletions cmd/spctl/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"encoding/json"
"net/http"
)

func getPluginVersion() (string, error) {
client := &http.Client{}

req, err := http.NewRequest("GET", "https://api.github.com/repos/koltyakov/cq-source-sharepoint/releases/latest", nil)
if err != nil {
return pluginVersion, err
}

req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")

resp, err := client.Do(req)
if err != nil {
return pluginVersion, err
}

defer resp.Body.Close()

var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return "", err
}

return data["tag_name"].(string), nil
}
50 changes: 50 additions & 0 deletions cmd/spctl/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"os"
"strings"
)

type SourceSpec struct {
Name string `json:"name"`
Registry string `json:"registry"`
Path string `json:"path"`
Version string `json:"version"`
Destinations []string `json:"destination"`
Spec PluginSpec `json:"spec"`
}

type PluginSpec struct {
Auth AuthSpec `json:"auth"`
}

type AuthSpec struct {
Strategy string `json:"strategy"`
Creds [][]string `json:"creds"`
}

func (s *SourceSpec) Marshal() []byte {
credsArray := ""
for _, c := range s.Spec.Auth.Creds {
credsArray += " " + c[0] + ": " + c[1] + "\n"
}
spec := strings.TrimSpace(`
kind: source
spec:
name: ` + s.Name + `
registry: ` + s.Registry + `
path: ` + s.Path + `
version: ` + s.Version + `
destination: ["` + strings.Join(s.Destinations, `", "`) + `"]
spec:
auth:
strategy: ` + s.Spec.Auth.Strategy + `
creds:
` + credsArray + `
`)
return []byte(spec)
}

func (s *SourceSpec) Save(filename string) error {
return os.WriteFile(filename, s.Marshal(), 0644)
}

0 comments on commit f5876f0

Please sign in to comment.