Skip to content

Commit

Permalink
Merge branch 'testcli'
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Dec 4, 2021
2 parents b041e7e + 118bba9 commit 893d54e
Show file tree
Hide file tree
Showing 14 changed files with 1,047 additions and 71 deletions.
11 changes: 11 additions & 0 deletions aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func (l *Lambda) arn(name string) string {
name)
}

func (l *Lambda) Info(name string) (map[string]string, error) {
gfi := &lambda.GetFunctionInput{
FunctionName: aws.String(name),
}
gfo, err := l.cli.GetFunction(context.Background(), gfi)
if err == nil {
return gfo.Tags, nil
}
return nil, err
}

func (l *Lambda) Invoke(name string, req, rsp interface{}, headers map[string]string) error {
var payload []byte
if req != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/config/state.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ stages:
default: true
node: production
endpoints:
rest: ""
ws: ""
rest: "https://ytg5gfkg5k.execute-api.eu-central-1.amazonaws.com"
ws: "wss://vbel3cs8ki.execute-api.eu-central-1.amazonaws.com"
functions:
- name: report
hash: caee300f
Expand Down
41 changes: 24 additions & 17 deletions cli/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,42 @@ var (
)

func newEventsCollector() *eventsCollector {
var disabled bool
if val, ok := os.LookupEnv(domain.EnvNoEvents); ok && val != "" {
disabled = true
}
ec := eventsCollector{
connectDone: make(chan struct{}),
store: newEventsStore(),
store: newEventsStore(),
disabled: disabled,
}

go func() {
defer close(ec.connectDone)
p, err := net.NewPublisher(secret.EventPublisherCreds)
if err != nil {
Error(fmt.Errorf("failed to start event publisher %w", err))
return
}
ec.publisher = p
}()
return &ec
}

type eventsCollector struct {
publisher *net.Publisher
connectDone chan struct{}
store *eventsStore
disabled bool
publisher *net.Publisher
store *eventsStore
}

func (c *eventsCollector) send() error {
if c.disabled {
return nil
}
if c.publisher == nil {
p, err := net.NewPublisher(secret.EventPublisherCreds)
if err != nil {
Error(fmt.Errorf("failed to start event publisher %w", err))
} else {
c.publisher = p
}
}

cliCommand.End()
buf, err := cliCommand.Marshal()
if err != nil {
c.store.push(buf)
return Wrap(err)
}
// wait for net connection to finish
<-c.connectDone
if c.publisher == nil {
c.store.push(buf)
return fmt.Errorf("publisher not found")
Expand All @@ -72,6 +76,9 @@ func (c *eventsCollector) send() error {
}

func (c *eventsCollector) close() error {
if c.disabled {
return nil
}
if err := c.send(); err != nil {
return c.store.store()
}
Expand Down
38 changes: 15 additions & 23 deletions domain/file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path"
"path/filepath"
"time"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -115,16 +114,20 @@ func AppConfigDir() (string, error) {
}

func StoreActivationToken(jwt string) error {
dir, err := AppConfigDir()
dir, err := activationTokenPath()
if err != nil {
return err
}
return StoreActivationTokenTo(jwt, dir)
}

func StoreActivationTokenTo(jwt string, dir string) error {
filename := path.Join(dir, activationTokenFilename)
return ioutil.WriteFile(filename, []byte(jwt), os.ModePerm)
}

func ReadActivationToken() (string, error) {
dir, err := AppConfigDir()
dir, err := activationTokenPath()
if err != nil {
return "", err
}
Expand All @@ -139,7 +142,14 @@ func ReadActivationToken() (string, error) {
return string(buf), nil
}

func workspacePathAndName() (string, string, error) {
func activationTokenPath() (string, error) {
if val, ok := os.LookupEnv(EnvWorkspacePath); ok {
return val, nil
}
return AppConfigDir()
}

func WorkspacePathAndName() (string, string, error) {
// workspace pats set from env, used in end_to_end test
if val, ok := os.LookupEnv(EnvWorkspacePath); ok {
return val, workspaceFilename, nil
Expand All @@ -155,27 +165,9 @@ func workspacePathAndName() (string, string, error) {
if err != nil {
return "", "", err
}
path := filepath.Join(apd, workspaceFilename)
if pathExists(path) {
return apd, workspaceFilename, nil
}
legacyPath := filepath.Join(apd, legacyWorkspaceName()+".yml")
if pathExists(legacyPath) {
upgradeWorkspace(legacyPath, path)
}
return apd, workspaceFilename, nil
}

func upgradeWorkspace(from, to string) {
var s FileStore
s.workspaceFile = from
_ = s.restoreWorkspace()
s.workspace.ID = UID()
s.workspace.CreatedAt = time.Now().UnixMilli()
s.workspaceFile = to
_ = s.storeWorkspace()
}

// NewSingleDeveloperWorkspaceStore loads workspace
// allows to be outside of project
func NewSingleDeveloperWorkspaceStore() (*FileStore, error) {
Expand All @@ -192,7 +184,7 @@ func newSingleDeveloper(mustFindProject bool) (*FileStore, error) {
if err != nil && mustFindProject {
return nil, err
}
workspacePath, workspaceFilename, err := workspacePathAndName()
workspacePath, workspaceFilename, err := WorkspacePathAndName()
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
32 changes: 3 additions & 29 deletions domain/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"encoding/hex"
"fmt"
"io"
"os/user"
"regexp"
"strings"
"time"

Expand All @@ -30,6 +28,9 @@ const (
TagKey = EnvKey
TagProjectName = EnvProjectName
TagStageName = EnvStageName

// set to non empty to disable sending events from cli
EnvNoEvents = "MANTIL_NO_EVENTS"
)

type Workspace struct {
Expand Down Expand Up @@ -210,33 +211,6 @@ func UID() string {
return string(buf)
}

func legacyWorkspaceName() string {
dflt := "workspace"

u, _ := user.Current()
if u == nil {
return dflt
}
username := u.Username
if strings.Contains(username, `\`) {
parts := strings.Split(username, `\`)
username = parts[len(parts)-1]
}
if username == "" {
return dflt
}
username = strings.ToLower(username)

// Make a Regex to say we only want letters and numbers
reg := regexp.MustCompile("[^a-z0-9]+")
username = reg.ReplaceAllString(username, "")

if username == "" {
return dflt
}
return username
}

func (w *Workspace) FindNode(name string) *Node {
if name == "" && len(w.Nodes) == 1 {
return w.Nodes[0]
Expand Down
Loading

0 comments on commit 893d54e

Please sign in to comment.