├── .gitignore ├── .golangci.yml ├── LICENSE ├── README.md ├── analytics ├── client.go ├── client_test.go ├── event.go ├── property.go ├── property_test.go ├── track.go └── track_test.go ├── bitrise.yml ├── command ├── command.go ├── command_test.go ├── errorcollector.go ├── errors.go └── testdata │ ├── exit_42.sh │ ├── exit_with_message.sh │ └── not_existing_executable.sh ├── env └── env.go ├── errorutil ├── formatted_error.go └── formatted_error_test.go ├── exitcode ├── exitcode.go └── exitcode_string.go ├── fileutil ├── fileutil.go └── fileutil_test.go ├── go.mod ├── go.sum ├── log ├── colorstring │ ├── colorstring.go │ └── colorstring_test.go ├── log.go ├── log_test.go └── severity.go ├── mocks ├── Client.go └── Logger.go ├── pathutil ├── pathutil.go └── pathutil_test.go ├── redactwriter ├── range.go ├── range_test.go ├── redactwriter.go └── redactwriter_test.go ├── renovate.json ├── retryhttp └── retryhttp.go └── system ├── cpu.go └── cpu_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | .bitrise* 2 | .gows.user.yml 3 | .DS_Store 4 | .idea 5 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | run: 2 | skip-dirs: 3 | - .*/mocks 4 | 5 | issues: 6 | # https://github.com/golangci/golangci-lint/issues/2439 7 | exclude-use-default: false 8 | 9 | linters: 10 | enable: 11 | - errcheck 12 | - gosimple 13 | - govet 14 | - ineffassign 15 | - staticcheck 16 | - typecheck 17 | - unused 18 | - revive 19 | 20 | linters-settings: 21 | revive: 22 | severity: error 23 | rules: 24 | - name: exported 25 | arguments: 26 | - checkPrivateReceivers 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Bitrise 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-utils 2 | Common, utility packages for Go 3 | -------------------------------------------------------------------------------- /analytics/client.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/bitrise-io/go-utils/v2/log" 10 | "github.com/bitrise-io/go-utils/v2/retryhttp" 11 | ) 12 | 13 | const trackEndpoint = "https://step-analytics.bitrise.io/track" 14 | 15 | // Client ... 16 | type Client interface { 17 | Send(buffer *bytes.Buffer) 18 | } 19 | 20 | type client struct { 21 | httpClient *http.Client 22 | timeout time.Duration 23 | endpoint string 24 | logger log.Logger 25 | } 26 | 27 | // NewDefaultClient ... 28 | func NewDefaultClient(logger log.Logger, timeout time.Duration) Client { 29 | httpClient := retryhttp.NewClient(logger).StandardClient() 30 | httpClient.Timeout = timeout 31 | return NewClient(httpClient, trackEndpoint, logger, timeout) 32 | } 33 | 34 | // NewClient ... 35 | func NewClient(httpClient *http.Client, endpoint string, logger log.Logger, timeout time.Duration) Client { 36 | return client{httpClient: httpClient, endpoint: endpoint, logger: logger, timeout: timeout} 37 | } 38 | 39 | // Send ... 40 | func (t client) Send(buffer *bytes.Buffer) { 41 | ctx, cancel := context.WithTimeout(context.Background(), t.timeout) 42 | defer cancel() 43 | 44 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.endpoint, buffer) 45 | if err != nil { 46 | t.logger.Warnf("Couldn't create analytics request: %s", err) 47 | } 48 | 49 | req.Header.Set("Content-Type", "application/json") 50 | 51 | res, err := t.httpClient.Do(req) 52 | if err != nil { 53 | t.logger.Debugf("Couldn't send analytics event: %s", err) 54 | return 55 | } 56 | 57 | defer func() { 58 | if err := res.Body.Close(); err != nil { 59 | t.logger.Debugf("Couldn't close anaytics body: %s", err) 60 | } 61 | }() 62 | 63 | if statusOK := res.StatusCode >= 200 && res.StatusCode < 300; !statusOK { 64 | t.logger.Debugf("Couldn't send analytics event, status code: %d", res.StatusCode) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /analytics/client_test.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "net/http" 7 | "net/http/httptest" 8 | "testing" 9 | "time" 10 | 11 | "github.com/bitrise-io/go-utils/v2/mocks" 12 | 13 | "github.com/stretchr/testify/assert" 14 | "github.com/stretchr/testify/mock" 15 | ) 16 | 17 | const cientTimeout = 10 * time.Second 18 | 19 | func Test_trackerClient_send_success(t *testing.T) { 20 | mockLogger := new(mocks.Logger) 21 | testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 22 | b, err := io.ReadAll(req.Body) 23 | assert.NoError(t, err) 24 | assert.Equal(t, string(b), "{}") 25 | assert.Equal(t, req.Method, http.MethodPost) 26 | assert.Equal(t, req.Header.Get("Content-Type"), "application/json") 27 | res.WriteHeader(200) 28 | _, err = res.Write([]byte("ok")) 29 | assert.NoError(t, err) 30 | })) 31 | defer func() { testServer.Close() }() 32 | client := NewClient(http.DefaultClient, testServer.URL, mockLogger, cientTimeout) 33 | client.Send(bytes.NewBufferString("{}")) 34 | mockLogger.AssertNotCalled(t, "Debugf", mock.Anything, mock.Anything) 35 | } 36 | 37 | func Test_trackerClient_send_failure(t *testing.T) { 38 | mockLogger := new(mocks.Logger) 39 | mockLogger.On("Debugf", mock.Anything, mock.Anything).Return() 40 | testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 41 | b, err := io.ReadAll(req.Body) 42 | assert.NoError(t, err) 43 | assert.Equal(t, string(b), "{}") 44 | assert.Equal(t, req.Method, http.MethodPost) 45 | assert.Equal(t, req.Header.Get("Content-Type"), "application/json") 46 | res.WriteHeader(500) 47 | _, err = res.Write([]byte("failure")) 48 | assert.NoError(t, err) 49 | })) 50 | defer func() { testServer.Close() }() 51 | client := NewClient(http.DefaultClient, testServer.URL, mockLogger, cientTimeout) 52 | client.Send(bytes.NewBufferString("{}")) 53 | mockLogger.AssertCalled(t, "Debugf", "Couldn't send analytics event, status code: %d", 500) 54 | } 55 | -------------------------------------------------------------------------------- /analytics/event.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io" 7 | "time" 8 | 9 | "github.com/gofrs/uuid/v5" 10 | ) 11 | 12 | type event struct { 13 | ID string `json:"id"` 14 | EventName string `json:"event_name"` 15 | Timestamp int64 `json:"timestamp"` 16 | Properties Properties `json:"properties"` 17 | } 18 | 19 | func newEvent(name string, properties []Properties) event { 20 | return event{ 21 | ID: uuid.Must(uuid.NewV4()).String(), 22 | EventName: name, 23 | Timestamp: time.Now().UnixNano() / int64(time.Microsecond), 24 | Properties: merge(properties), 25 | } 26 | } 27 | 28 | func (e event) toJSON(writer io.Writer) { 29 | if err := json.NewEncoder(writer).Encode(e); err != nil { 30 | panic(fmt.Sprintf("Analytics event should be serializable to JSON: %s", err.Error())) 31 | } 32 | } 33 | 34 | func merge(properties []Properties) Properties { 35 | if len(properties) == 0 { 36 | return nil 37 | } 38 | m := map[string]interface{}{} 39 | for _, p := range properties { 40 | for key, value := range p { 41 | m[key] = value 42 | } 43 | } 44 | return m 45 | } 46 | -------------------------------------------------------------------------------- /analytics/property.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "github.com/mitchellh/mapstructure" 5 | ) 6 | 7 | // Properties ... 8 | type Properties map[string]interface{} 9 | 10 | // Merge ... 11 | func (p Properties) Merge(properties Properties) Properties { 12 | r := Properties{} 13 | for key, value := range p { 14 | r[key] = value 15 | } 16 | for key, value := range properties { 17 | r[key] = value 18 | } 19 | return r 20 | } 21 | 22 | // AppendIfNotEmpty ... 23 | func (p Properties) AppendIfNotEmpty(key string, value string) { 24 | if value != "" { 25 | p[key] = value 26 | } 27 | } 28 | 29 | // NewProperty Converts struct into Properties map based on json tags 30 | func NewProperty(item interface{}) Properties { 31 | result := Properties{} 32 | decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ 33 | TagName: "json", 34 | Result: &result, 35 | }) 36 | if err != nil { 37 | return Properties{} 38 | } 39 | if err := decoder.Decode(item); err != nil { 40 | return Properties{} 41 | } 42 | return result 43 | } 44 | -------------------------------------------------------------------------------- /analytics/property_test.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | type simpleTestStruct struct { 10 | Name string `json:"name"` 11 | Address string `json:"address,omitempty"` 12 | Age int `json:"age"` 13 | Happy bool `json:"happy"` 14 | NotJSONField string 15 | IgnoredJSONField string `json:"-"` 16 | } 17 | 18 | type complexTestStruct struct { 19 | Name string `json:"name"` 20 | Address string `json:"address,omitempty"` 21 | City string `json:"city"` 22 | Age int `json:"age"` 23 | Happy bool `json:"happy"` 24 | Cars []string `json:"cars"` 25 | ColorCollection map[string]bool `json:"color_collection"` 26 | } 27 | 28 | type emptyTestStruct struct { 29 | } 30 | 31 | func TestNewProperty(t *testing.T) { 32 | tests := []struct { 33 | name string 34 | item interface{} 35 | want Properties 36 | }{ 37 | { 38 | name: "Simple Test", 39 | item: simpleTestStruct{ 40 | Name: "John", 41 | Age: 42, 42 | Happy: true, 43 | NotJSONField: "Not a json field", 44 | }, 45 | want: Properties{ 46 | "name": "John", 47 | "age": 42, 48 | "happy": true, 49 | "NotJSONField": "Not a json field", 50 | }, 51 | }, 52 | { 53 | name: "Complex Test", 54 | item: complexTestStruct{ 55 | Name: "John", 56 | Age: 42, 57 | Happy: true, 58 | Cars: []string{"Ford", "Chevy"}, 59 | ColorCollection: map[string]bool{"red": true, "blue": false}, 60 | }, 61 | want: Properties{ 62 | "name": "John", 63 | "city": "", 64 | "age": 42, 65 | "happy": true, 66 | "cars": []string{"Ford", "Chevy"}, 67 | "color_collection": map[string]bool{"red": true, "blue": false}, 68 | }, 69 | }, 70 | { 71 | name: "Empty Test", 72 | item: emptyTestStruct{}, 73 | want: Properties{}, 74 | }, 75 | } 76 | for _, tt := range tests { 77 | t.Run(tt.name, func(t *testing.T) { 78 | assert.Equalf(t, tt.want, NewProperty(tt.item), "NewProperty(%v)", tt.item) 79 | }) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /analytics/track.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "bytes" 5 | "sync" 6 | "time" 7 | 8 | "github.com/bitrise-io/go-utils/v2/env" 9 | "github.com/bitrise-io/go-utils/v2/log" 10 | ) 11 | 12 | const poolSize = 10 13 | const bufferSize = 100 14 | const timeout = 30 * time.Second 15 | const asyncClientTimeout = 30 * time.Second 16 | const analyticsDisabledEnv = "ANALYTICS_DISABLED" 17 | 18 | // Tracker ... 19 | type Tracker interface { 20 | Enqueue(eventName string, properties ...Properties) 21 | Wait() 22 | IsTracking() bool 23 | } 24 | 25 | type tracker struct { 26 | jobs chan *bytes.Buffer 27 | waitGroup *sync.WaitGroup 28 | client Client 29 | properties []Properties 30 | waitTimeout time.Duration 31 | } 32 | 33 | type noopTracker struct{} 34 | 35 | // Enqueue ... 36 | func (t noopTracker) Enqueue(eventName string, properties ...Properties) {} 37 | 38 | // Wait ... 39 | func (t noopTracker) Wait() {} 40 | 41 | // IsTracking ... 42 | func (t noopTracker) IsTracking() bool { 43 | return false 44 | } 45 | 46 | // NewDefaultTracker ... 47 | func NewDefaultTracker(logger log.Logger, envRepo env.Repository, properties ...Properties) Tracker { 48 | if envRepo.Get(analyticsDisabledEnv) == "true" { 49 | return noopTracker{} 50 | } 51 | return NewTracker(NewDefaultClient(logger, asyncClientTimeout), timeout, properties...) 52 | } 53 | 54 | // NewTracker ... 55 | func NewTracker(client Client, waitTimeout time.Duration, properties ...Properties) Tracker { 56 | t := tracker{client: client, jobs: make(chan *bytes.Buffer, bufferSize), waitGroup: &sync.WaitGroup{}, properties: properties, waitTimeout: waitTimeout} 57 | t.init(poolSize) 58 | return &t 59 | } 60 | 61 | // Enqueue ... 62 | func (t tracker) Enqueue(eventName string, properties ...Properties) { 63 | var b bytes.Buffer 64 | newEvent(eventName, append(t.properties, properties...)).toJSON(&b) 65 | t.waitGroup.Add(1) 66 | t.jobs <- &b 67 | } 68 | 69 | // Wait ... 70 | func (t tracker) Wait() { 71 | close(t.jobs) 72 | c := make(chan struct{}) 73 | go func() { 74 | defer close(c) 75 | t.waitGroup.Wait() 76 | }() 77 | select { 78 | case <-c: 79 | case <-time.After(t.waitTimeout): 80 | } 81 | } 82 | 83 | // IsTracking ... 84 | func (t tracker) IsTracking() bool { 85 | return true 86 | } 87 | 88 | func (t tracker) init(size int) { 89 | for i := 0; i < size; i++ { 90 | go t.worker() 91 | } 92 | } 93 | 94 | func (t tracker) worker() { 95 | for job := range t.jobs { 96 | t.client.Send(job) 97 | t.waitGroup.Done() 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /analytics/track_test.go: -------------------------------------------------------------------------------- 1 | package analytics 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "testing" 7 | "time" 8 | 9 | "github.com/bitrise-io/go-utils/v2/env" 10 | "github.com/bitrise-io/go-utils/v2/mocks" 11 | 12 | "github.com/stretchr/testify/mock" 13 | ) 14 | 15 | func Test_tracker_EnqueueWaitCycleExecutesSends(t *testing.T) { 16 | mockClient := new(mocks.Client) 17 | mockClient.On("Send", mock.Anything).Return() 18 | 19 | tracker := NewTracker(mockClient, timeout) 20 | tracker.Enqueue("first") 21 | tracker.Enqueue("second") 22 | tracker.Enqueue("third") 23 | tracker.Enqueue("fourth") 24 | tracker.Enqueue("fifth") 25 | tracker.Wait() 26 | 27 | mockClient.AssertNumberOfCalls(t, "Send", 5) 28 | } 29 | 30 | func Test_tracker_SendIsCalledWithExpectedData(t *testing.T) { 31 | mockClient := new(mocks.Client) 32 | mockClient.On("Send", mock.Anything).Return() 33 | 34 | tracker := NewTracker(mockClient, timeout) 35 | baseProperties := Properties{"session": "id"} 36 | tracker.Enqueue( 37 | "first", 38 | baseProperties, Properties{ 39 | "property": "value", 40 | "intproperty": 42, 41 | "longproperty": 42, 42 | "floatproperty": 3.14, 43 | "boolproperty": true, 44 | "property2": Properties{"foo": "bar"}, 45 | }, 46 | ) 47 | tracker.Wait() 48 | 49 | matcher := mock.MatchedBy(func(buffer *bytes.Buffer) bool { 50 | var event event 51 | err := json.Unmarshal(buffer.Bytes(), &event) 52 | if err != nil { 53 | return false 54 | } 55 | if event.EventName != "first" { 56 | return false 57 | } 58 | if len(event.Properties) != 7 || 59 | event.Properties["property"] != "value" || 60 | event.Properties["intproperty"].(float64) != 42 || 61 | event.Properties["longproperty"].(float64) != 42 || 62 | event.Properties["floatproperty"].(float64) != 3.14 || 63 | event.Properties["boolproperty"].(bool) != true || 64 | event.Properties["session"] != "id" || 65 | event.Properties["property2"].(map[string]interface{})["foo"] != "bar" { 66 | return false 67 | } 68 | if event.ID == "" || event.Timestamp == 0 { 69 | return false 70 | } 71 | return true 72 | }) 73 | mockClient.AssertNumberOfCalls(t, "Send", 1) 74 | mockClient.AssertCalled(t, "Send", matcher) 75 | } 76 | 77 | func Test_tracker_MergingPropertiesWork(t *testing.T) { 78 | mockClient := new(mocks.Client) 79 | mockClient.On("Send", mock.Anything).Return() 80 | 81 | tracker := NewTracker(mockClient, timeout, Properties{"base": "base"}) 82 | baseProperties := Properties{"first": "first"} 83 | tracker.Enqueue("event", baseProperties) 84 | newBaseProperties := baseProperties.Merge(Properties{"second": "second"}) 85 | tracker.Enqueue("event2", newBaseProperties) 86 | tracker.Wait() 87 | 88 | mockClient.AssertNumberOfCalls(t, "Send", 2) 89 | matcher := mock.MatchedBy(func(buffer *bytes.Buffer) bool { 90 | var event event 91 | err := json.Unmarshal(buffer.Bytes(), &event) 92 | if err != nil { 93 | return false 94 | } 95 | if event.EventName == "event" { 96 | if len(event.Properties) != 2 || event.Properties["base"] != "base" || event.Properties["first"] != "first" { 97 | return false 98 | } 99 | return true 100 | } 101 | if event.EventName == "event2" { 102 | if len(event.Properties) != 3 || event.Properties["base"] != "base" || event.Properties["first"] != "first" || event.Properties["second"] != "second" { 103 | return false 104 | } 105 | return true 106 | } 107 | return false 108 | }) 109 | mockClient.AssertCalled(t, "Send", matcher) 110 | } 111 | 112 | func Test_tracker_WaitTimesOutOnBlockingClient(t *testing.T) { 113 | timeout := time.After(3 * time.Second) 114 | done := make(chan bool) 115 | go func() { 116 | mockClient := new(mocks.Client) 117 | mockClient.On("Send", mock.Anything).Run(func(args mock.Arguments) { 118 | select {} 119 | }) 120 | tracker := NewTracker(mockClient, 2*time.Second) 121 | tracker.Enqueue("block") 122 | tracker.Wait() 123 | done <- true 124 | }() 125 | 126 | select { 127 | case <-timeout: 128 | t.Fatal("Test didn't finish in time") 129 | case <-done: 130 | } 131 | } 132 | 133 | func Test_NewDefaultTracker_Disabled(t *testing.T) { 134 | t.Setenv(analyticsDisabledEnv, "true") 135 | 136 | tracker := NewDefaultTracker(new(mocks.Logger), env.NewRepository()) 137 | 138 | if _, ok := tracker.(noopTracker); !ok { 139 | t.Fatalf("expected noopTracker when %s is set", analyticsDisabledEnv) 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /bitrise.yml: -------------------------------------------------------------------------------- 1 | format_version: "11" 2 | default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git 3 | 4 | workflows: 5 | test: 6 | steps: 7 | - git::https://github.com/bitrise-steplib/steps-check.git: 8 | title: Lint 9 | inputs: 10 | - workflow: lint 11 | - skip_step_yml_validation: "yes" 12 | - go-list: { } 13 | - go-test: { } 14 | -------------------------------------------------------------------------------- /command/command.go: -------------------------------------------------------------------------------- 1 | package command 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "os/exec" 8 | "strings" 9 | 10 | "github.com/bitrise-io/go-utils/v2/env" 11 | ) 12 | 13 | // ErrorFinder ... 14 | type ErrorFinder func(out string) []string 15 | 16 | // Opts ... 17 | type Opts struct { 18 | Stdout io.Writer 19 | Stderr io.Writer 20 | Stdin io.Reader 21 | Env []string 22 | Dir string 23 | ErrorFinder ErrorFinder 24 | } 25 | 26 | // Factory ... 27 | type Factory interface { 28 | Create(name string, args []string, opts *Opts) Command 29 | } 30 | 31 | type factory struct { 32 | envRepository env.Repository 33 | } 34 | 35 | // NewFactory ... 36 | func NewFactory(envRepository env.Repository) Factory { 37 | return factory{envRepository: envRepository} 38 | } 39 | 40 | // Create ... 41 | func (f factory) Create(name string, args []string, opts *Opts) Command { 42 | cmd := exec.Command(name, args...) 43 | var collector *errorCollector 44 | 45 | if opts != nil { 46 | if opts.ErrorFinder != nil { 47 | collector = &errorCollector{errorFinder: opts.ErrorFinder} 48 | } 49 | 50 | cmd.Stdout = opts.Stdout 51 | cmd.Stderr = opts.Stderr 52 | cmd.Stdin = opts.Stdin 53 | 54 | // If Env is nil, the new process uses the current process's 55 | // environment. 56 | // If we pass env vars we want to append them to the 57 | // current process's environment. 58 | cmd.Env = append(f.envRepository.List(), opts.Env...) 59 | cmd.Dir = opts.Dir 60 | } 61 | return &command{ 62 | cmd: cmd, 63 | errorCollector: collector, 64 | } 65 | } 66 | 67 | // Command ... 68 | type Command interface { 69 | PrintableCommandArgs() string 70 | Run() error 71 | RunAndReturnExitCode() (int, error) 72 | RunAndReturnTrimmedOutput() (string, error) 73 | RunAndReturnTrimmedCombinedOutput() (string, error) 74 | Start() error 75 | Wait() error 76 | } 77 | 78 | type command struct { 79 | cmd *exec.Cmd 80 | errorCollector *errorCollector 81 | } 82 | 83 | // PrintableCommandArgs ... 84 | func (c command) PrintableCommandArgs() string { 85 | return printableCommandArgs(false, c.cmd.Args) 86 | } 87 | 88 | // Run ... 89 | func (c *command) Run() error { 90 | c.wrapOutputs() 91 | 92 | if err := c.cmd.Run(); err != nil { 93 | return c.wrapError(err) 94 | } 95 | 96 | return nil 97 | } 98 | 99 | // RunAndReturnExitCode ... 100 | func (c command) RunAndReturnExitCode() (int, error) { 101 | c.wrapOutputs() 102 | err := c.cmd.Run() 103 | if err != nil { 104 | err = c.wrapError(err) 105 | } 106 | 107 | exitCode := c.cmd.ProcessState.ExitCode() 108 | return exitCode, err 109 | } 110 | 111 | // RunAndReturnTrimmedOutput ... 112 | func (c command) RunAndReturnTrimmedOutput() (string, error) { 113 | outBytes, err := c.cmd.Output() 114 | outStr := string(outBytes) 115 | if err != nil { 116 | if c.errorCollector != nil { 117 | c.errorCollector.collectErrors(outStr) 118 | } 119 | err = c.wrapError(err) 120 | } 121 | 122 | return strings.TrimSpace(outStr), err 123 | } 124 | 125 | // RunAndReturnTrimmedCombinedOutput ... 126 | func (c command) RunAndReturnTrimmedCombinedOutput() (string, error) { 127 | outBytes, err := c.cmd.CombinedOutput() 128 | outStr := string(outBytes) 129 | if err != nil { 130 | if c.errorCollector != nil { 131 | c.errorCollector.collectErrors(outStr) 132 | } 133 | err = c.wrapError(err) 134 | } 135 | 136 | return strings.TrimSpace(outStr), err 137 | } 138 | 139 | // Start ... 140 | func (c command) Start() error { 141 | c.wrapOutputs() 142 | return c.cmd.Start() 143 | } 144 | 145 | // Wait ... 146 | func (c command) Wait() error { 147 | err := c.cmd.Wait() 148 | if err != nil { 149 | err = c.wrapError(err) 150 | } 151 | 152 | return err 153 | } 154 | 155 | func printableCommandArgs(isQuoteFirst bool, fullCommandArgs []string) string { 156 | var cmdArgsDecorated []string 157 | for idx, anArg := range fullCommandArgs { 158 | quotedArg := fmt.Sprintf("\"%s\"", anArg) 159 | if idx == 0 && !isQuoteFirst { 160 | quotedArg = anArg 161 | } 162 | cmdArgsDecorated = append(cmdArgsDecorated, quotedArg) 163 | } 164 | 165 | return strings.Join(cmdArgsDecorated, " ") 166 | } 167 | 168 | func (c command) wrapError(err error) error { 169 | var exitErr *exec.ExitError 170 | if errors.As(err, &exitErr) { 171 | errorLines := []string{} 172 | if c.errorCollector != nil { 173 | errorLines = c.errorCollector.errorLines 174 | } 175 | 176 | return NewExitStatusError(c.PrintableCommandArgs(), exitErr, errorLines) 177 | } 178 | 179 | return fmt.Errorf("executing command failed (%s): %w", c.PrintableCommandArgs(), err) 180 | } 181 | 182 | func (c command) wrapOutputs() { 183 | if c.errorCollector == nil { 184 | return 185 | } 186 | 187 | if c.cmd.Stdout != nil { 188 | outWriter := io.MultiWriter(c.errorCollector, c.cmd.Stdout) 189 | c.cmd.Stdout = outWriter 190 | } else { 191 | c.cmd.Stdout = c.errorCollector 192 | } 193 | 194 | if c.cmd.Stderr != nil { 195 | errWriter := io.MultiWriter(c.errorCollector, c.cmd.Stderr) 196 | c.cmd.Stderr = errWriter 197 | } else { 198 | c.cmd.Stderr = c.errorCollector 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /command/command_test.go: -------------------------------------------------------------------------------- 1 | package command 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "fmt" 7 | "os/exec" 8 | "strings" 9 | "testing" 10 | 11 | "github.com/bitrise-io/go-utils/v2/env" 12 | "github.com/stretchr/testify/assert" 13 | ) 14 | 15 | func TestRunErrors(t *testing.T) { 16 | tests := []struct { 17 | name string 18 | cmd command 19 | wantErr string 20 | }{ 21 | { 22 | name: "command without stdout set", 23 | cmd: command{cmd: exec.Command("bash", "testdata/exit_with_message.sh")}, 24 | wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): check the command's output for details`, 25 | }, 26 | { 27 | name: "command with stdout set", 28 | cmd: func() command { 29 | c := exec.Command("bash", "testdata/exit_with_message.sh") 30 | var out bytes.Buffer 31 | c.Stdout = &out 32 | return command{cmd: c} 33 | }(), 34 | wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): check the command's output for details`, 35 | }, 36 | { 37 | name: "command with error finder", 38 | cmd: func() command { 39 | c := exec.Command("bash", "testdata/exit_with_message.sh") 40 | errorFinder := func(out string) []string { 41 | var errors []string 42 | for _, line := range strings.Split(out, "\n") { 43 | if strings.Contains(line, "Error:") { 44 | errors = append(errors, line) 45 | } 46 | } 47 | return errors 48 | } 49 | 50 | return command{ 51 | cmd: c, 52 | errorCollector: &errorCollector{errorFinder: errorFinder}, 53 | } 54 | }(), 55 | wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error 56 | Error: second error 57 | Error: third error 58 | Error: fourth error`, 59 | }, 60 | } 61 | for _, tt := range tests { 62 | t.Run(tt.name, func(t *testing.T) { 63 | err := tt.cmd.Run() 64 | var gotErrMsg string 65 | if err != nil { 66 | gotErrMsg = err.Error() 67 | } 68 | if gotErrMsg != tt.wantErr { 69 | t.Errorf("command.Run() error = \n%v\n, wantErr \n%v\n", gotErrMsg, tt.wantErr) 70 | return 71 | } 72 | }) 73 | } 74 | } 75 | 76 | func TestRunCmdAndReturnExitCode(t *testing.T) { 77 | type args struct { 78 | cmd Command 79 | } 80 | factory := NewFactory(env.NewRepository()) 81 | tests := []struct { 82 | name string 83 | args args 84 | wantExitCode int 85 | wantErr bool 86 | }{ 87 | { 88 | name: "invalid command", 89 | args: args{ 90 | cmd: factory.Create("", nil, nil), 91 | }, 92 | wantExitCode: -1, 93 | wantErr: true, 94 | }, 95 | { 96 | name: "env command", 97 | args: args{ 98 | cmd: factory.Create("env", nil, nil), 99 | }, 100 | wantExitCode: 0, 101 | wantErr: false, 102 | }, 103 | { 104 | name: "not existing executable", 105 | args: args{ 106 | cmd: factory.Create("bash", []string{"testdata/not_existing_executable.sh"}, nil), 107 | }, 108 | wantExitCode: 127, 109 | wantErr: true, 110 | }, 111 | { 112 | name: "exit 42", 113 | args: args{ 114 | cmd: factory.Create("bash", []string{"testdata/exit_42.sh"}, nil), 115 | }, 116 | wantExitCode: 42, 117 | wantErr: true, 118 | }, 119 | } 120 | for _, tt := range tests { 121 | t.Run(tt.name, func(t *testing.T) { 122 | gotExitCode, err := tt.args.cmd.RunAndReturnExitCode() 123 | if (err != nil) != tt.wantErr { 124 | t.Errorf("command.RunAndReturnExitCode() error = %v, wantErr %v", err, tt.wantErr) 125 | return 126 | } 127 | if tt.wantErr && tt.wantExitCode > 0 { 128 | var exitErr *exec.ExitError 129 | 130 | if ok := errors.As(err, &exitErr); !ok { 131 | t.Errorf("command.RunAndReturnExitCode() did nor return ExitError type: %s", err) 132 | return 133 | } 134 | 135 | if exitErr.ExitCode() != tt.wantExitCode { 136 | t.Errorf("command.RunAndReturnExitCode() exit code = %v, want %v", exitErr.ExitCode(), tt.wantExitCode) 137 | } 138 | } 139 | if gotExitCode != tt.wantExitCode { 140 | t.Errorf("command.RunAndReturnExitCode() = %v, want %v", gotExitCode, tt.wantExitCode) 141 | } 142 | }) 143 | } 144 | } 145 | 146 | func TestRunAndReturnTrimmedOutput(t *testing.T) { 147 | tests := []struct { 148 | name string 149 | cmd command 150 | wantErr string 151 | }{ 152 | { 153 | name: "command without error finder (falling back to exec.ExitError error output)", 154 | cmd: func() command { 155 | c := exec.Command("bash", "testdata/exit_with_message.sh") 156 | return command{ 157 | cmd: c, 158 | } 159 | }(), 160 | wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: third error 161 | Error: fourth error 162 | `, 163 | }, 164 | { 165 | name: "command with error finder", 166 | cmd: func() command { 167 | c := exec.Command("bash", "testdata/exit_with_message.sh") 168 | errorFinder := func(out string) []string { 169 | var errors []string 170 | for _, line := range strings.Split(out, "\n") { 171 | if strings.Contains(line, "Error:") { 172 | errors = append(errors, line) 173 | } 174 | } 175 | return errors 176 | } 177 | 178 | return command{ 179 | cmd: c, 180 | errorCollector: &errorCollector{errorFinder: errorFinder}, 181 | } 182 | }(), 183 | wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error 184 | Error: second error`, 185 | }, 186 | } 187 | for _, tt := range tests { 188 | t.Run(tt.name, func(t *testing.T) { 189 | _, err := tt.cmd.RunAndReturnTrimmedOutput() 190 | var gotErrMsg string 191 | if err != nil { 192 | gotErrMsg = err.Error() 193 | } 194 | if gotErrMsg != tt.wantErr { 195 | t.Errorf("command.Run() error = %v, wantErr %v", gotErrMsg, tt.wantErr) 196 | return 197 | } 198 | }) 199 | } 200 | } 201 | 202 | func TestRunAndReturnTrimmedCombinedOutput(t *testing.T) { 203 | tests := []struct { 204 | name string 205 | cmd command 206 | wantErr string 207 | }{ 208 | { 209 | name: "command without error finder", 210 | cmd: func() command { 211 | c := exec.Command("bash", "testdata/exit_with_message.sh") 212 | return command{ 213 | cmd: c, 214 | } 215 | }(), 216 | wantErr: "command failed with exit status 1 (bash \"testdata/exit_with_message.sh\"): check the command's output for details", 217 | }, 218 | { 219 | name: "command with error finder", 220 | cmd: func() command { 221 | c := exec.Command("bash", "testdata/exit_with_message.sh") 222 | errorFinder := func(out string) []string { 223 | var errors []string 224 | for _, line := range strings.Split(out, "\n") { 225 | if strings.Contains(line, "Error:") { 226 | errors = append(errors, line) 227 | } 228 | } 229 | return errors 230 | } 231 | 232 | return command{ 233 | cmd: c, 234 | errorCollector: &errorCollector{errorFinder: errorFinder}, 235 | } 236 | }(), 237 | wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error 238 | Error: second error 239 | Error: third error 240 | Error: fourth error`, 241 | }, 242 | } 243 | for _, tt := range tests { 244 | t.Run(tt.name, func(t *testing.T) { 245 | _, err := tt.cmd.RunAndReturnTrimmedCombinedOutput() 246 | var gotErrMsg string 247 | if err != nil { 248 | gotErrMsg = err.Error() 249 | } 250 | if gotErrMsg != tt.wantErr { 251 | t.Errorf("command.Run() error = %v, wantErr %v", gotErrMsg, tt.wantErr) 252 | return 253 | } 254 | }) 255 | } 256 | } 257 | 258 | func TestSpecialCharactersAreNotEscaped(t *testing.T) { 259 | programName := "test" 260 | argument := `-----BEGIN PRIVATE KEY-----\nThis\nis\na\nprivate-key\n-----END PRIVATE KEY-----` 261 | 262 | got := NewFactory(env.NewRepository()).Create(programName, []string{argument}, nil).PrintableCommandArgs() 263 | expected := fmt.Sprintf("%s \"%s\"", programName, argument) 264 | 265 | assert.Equal(t, expected, got) 266 | } 267 | -------------------------------------------------------------------------------- /command/errorcollector.go: -------------------------------------------------------------------------------- 1 | package command 2 | 3 | type errorCollector struct { 4 | errorLines []string 5 | errorFinder ErrorFinder 6 | } 7 | 8 | func (e *errorCollector) Write(p []byte) (n int, err error) { 9 | e.collectErrors(string(p)) 10 | return len(p), nil 11 | } 12 | 13 | func (e *errorCollector) collectErrors(output string) { 14 | lines := e.errorFinder(output) 15 | if len(lines) > 0 { 16 | e.errorLines = append(e.errorLines, lines...) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /command/errors.go: -------------------------------------------------------------------------------- 1 | package command 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "os/exec" 7 | "strings" 8 | ) 9 | 10 | // ExitStatusError ... 11 | type ExitStatusError struct { 12 | readableReason error 13 | originalExitErr error 14 | } 15 | 16 | // NewExitStatusError ... 17 | func NewExitStatusError(printableCmdArgs string, exitErr *exec.ExitError, errorLines []string) error { 18 | reasonMsg := fmt.Sprintf("command failed with exit status %d (%s)", exitErr.ExitCode(), printableCmdArgs) 19 | 20 | errorOutput := strings.Join(errorLines, "\n") 21 | if len(errorOutput) == 0 { 22 | if len(exitErr.Stderr) != 0 { 23 | errorOutput = string(exitErr.Stderr) 24 | } else { 25 | errorOutput = "check the command's output for details" 26 | } 27 | } 28 | 29 | return &ExitStatusError{ 30 | readableReason: fmt.Errorf("%s: %w", reasonMsg, errors.New(errorOutput)), 31 | originalExitErr: exitErr, 32 | } 33 | } 34 | 35 | // Error returns the formatted error message. Does not include the original error message (`exit status 1`). 36 | func (e *ExitStatusError) Error() string { 37 | return e.readableReason.Error() 38 | } 39 | 40 | // Unwrap is needed for errors.Is and errors.As to work correctly. 41 | func (e *ExitStatusError) Unwrap() error { 42 | return e.originalExitErr 43 | } 44 | 45 | // Reason returns the user-friendly error, to be used by errorutil.ErrorFormatter. 46 | func (e *ExitStatusError) Reason() error { 47 | return e.readableReason 48 | } 49 | -------------------------------------------------------------------------------- /command/testdata/exit_42.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | exit 42 -------------------------------------------------------------------------------- /command/testdata/exit_with_message.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # These messages are written to stdout 3 | echo Error: first error 4 | echo Error: second error 5 | echo This is not an stdout error 6 | # These messages are written to stderr 7 | echo Error: third error >&2 8 | echo Error: fourth error >&2 9 | exit 1 -------------------------------------------------------------------------------- /command/testdata/not_existing_executable.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | ./not_existing_bin -------------------------------------------------------------------------------- /env/env.go: -------------------------------------------------------------------------------- 1 | package env 2 | 3 | import ( 4 | "os" 5 | "os/exec" 6 | ) 7 | 8 | // CommandLocator ... 9 | type CommandLocator interface { 10 | LookPath(file string) (string, error) 11 | } 12 | 13 | type commandLocator struct{} 14 | 15 | // NewCommandLocator ... 16 | func NewCommandLocator() CommandLocator { 17 | return commandLocator{} 18 | } 19 | 20 | // LookPath ... 21 | func (l commandLocator) LookPath(file string) (string, error) { 22 | return exec.LookPath(file) 23 | } 24 | 25 | // Repository ... 26 | type Repository interface { 27 | List() []string 28 | Unset(key string) error 29 | Get(key string) string 30 | Set(key, value string) error 31 | } 32 | 33 | // NewRepository ... 34 | func NewRepository() Repository { 35 | return repository{} 36 | } 37 | 38 | type repository struct{} 39 | 40 | // Get ... 41 | func (d repository) Get(key string) string { 42 | return os.Getenv(key) 43 | } 44 | 45 | // Set ... 46 | func (d repository) Set(key, value string) error { 47 | return os.Setenv(key, value) 48 | } 49 | 50 | // Unset ... 51 | func (d repository) Unset(key string) error { 52 | return os.Unsetenv(key) 53 | } 54 | 55 | // List ... 56 | func (d repository) List() []string { 57 | return os.Environ() 58 | } 59 | -------------------------------------------------------------------------------- /errorutil/formatted_error.go: -------------------------------------------------------------------------------- 1 | package errorutil 2 | 3 | import ( 4 | "errors" 5 | "strings" 6 | 7 | "github.com/bitrise-io/go-utils/v2/command" 8 | ) 9 | 10 | // FormattedError ... 11 | func FormattedError(err error) string { 12 | var formatted string 13 | 14 | i := -1 15 | for { 16 | i++ 17 | 18 | // Use the user-friendly error message, ignore the original exec.ExitError. 19 | if commandExitStatusError, ok := err.(*command.ExitStatusError); ok { 20 | err = commandExitStatusError.Reason() 21 | } 22 | 23 | reason := err.Error() 24 | if err = errors.Unwrap(err); err == nil { 25 | formatted = appendError(formatted, reason, i, true) 26 | return formatted 27 | } 28 | 29 | reason = strings.TrimSuffix(reason, err.Error()) 30 | reason = strings.TrimRight(reason, " ") 31 | reason = strings.TrimSuffix(reason, ":") 32 | 33 | formatted = appendError(formatted, reason, i, false) 34 | } 35 | } 36 | 37 | func appendError(errorMessage, reason string, i int, last bool) string { 38 | if i == 0 { 39 | errorMessage = indentedReason(reason, i) 40 | } else { 41 | errorMessage += "\n" 42 | errorMessage += indentedReason(reason, i) 43 | } 44 | 45 | if !last { 46 | errorMessage += ":" 47 | } 48 | 49 | return errorMessage 50 | } 51 | 52 | func indentedReason(reason string, level int) string { 53 | var lines []string 54 | split := strings.Split(reason, "\n") 55 | for _, line := range split { 56 | line = strings.TrimLeft(line, " ") 57 | line = strings.TrimRight(line, "\n") 58 | line = strings.TrimRight(line, " ") 59 | if line == "" { 60 | continue 61 | } 62 | lines = append(lines, line) 63 | } 64 | 65 | var indented string 66 | for i, line := range lines { 67 | indented += strings.Repeat(" ", level) 68 | indented += line 69 | if i != len(lines)-1 { 70 | indented += "\n" 71 | } 72 | } 73 | return indented 74 | } 75 | -------------------------------------------------------------------------------- /errorutil/formatted_error_test.go: -------------------------------------------------------------------------------- 1 | package errorutil 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "strings" 7 | "testing" 8 | 9 | "github.com/bitrise-io/go-utils/v2/command" 10 | "github.com/bitrise-io/go-utils/v2/env" 11 | "github.com/stretchr/testify/require" 12 | ) 13 | 14 | func TestFormattedError(t *testing.T) { 15 | tests := []struct { 16 | name string 17 | error func() error 18 | wantFormattedError string 19 | }{ 20 | { 21 | name: "Single error", 22 | error: func() error { 23 | return errors.New("this is a failed error") 24 | }, 25 | wantFormattedError: "this is a failed error", 26 | }, 27 | { 28 | name: "Single multiline error", 29 | error: func() error { 30 | return errors.New("this is a failed error\nAnother line\nanother line") 31 | }, 32 | wantFormattedError: "this is a failed error\nAnother line\nanother line", 33 | }, 34 | { 35 | name: "Multiple wrapped errors", 36 | error: func() error { 37 | err := errors.New("the magic has failed") 38 | err = fmt.Errorf("second layer also failed: %w", err) 39 | err = fmt.Errorf("third layer also failed: %w", err) 40 | err = fmt.Errorf("fourth layer also failed: %w", err) 41 | return err 42 | }, wantFormattedError: `fourth layer also failed: 43 | third layer also failed: 44 | second layer also failed: 45 | the magic has failed`, 46 | }, 47 | { 48 | name: "Multiple non-wrapped errors", 49 | error: func() error { 50 | err := errors.New("the magic has failed") 51 | err = fmt.Errorf("second layer also failed: %s", err) 52 | err = fmt.Errorf("third layer also failed: %s", err) 53 | err = fmt.Errorf("fourth layer also failed: %s", err) 54 | return err 55 | }, wantFormattedError: "fourth layer also failed: third layer also failed: second layer also failed: the magic has failed", 56 | }, 57 | } 58 | for _, tt := range tests { 59 | t.Run(tt.name, func(t *testing.T) { 60 | formatted := FormattedError(tt.error()) 61 | 62 | if formatted != tt.wantFormattedError { 63 | t.Errorf("got formatted error = %s, want %s", formatted, tt.wantFormattedError) 64 | } 65 | }) 66 | } 67 | } 68 | 69 | func TestFormattedErrorWithCommand(t *testing.T) { 70 | commandFactory := command.NewFactory(env.NewRepository()) 71 | 72 | tests := []struct { 73 | name string 74 | cmdFn func() error 75 | wantErr string 76 | wantMsg string 77 | }{ 78 | { 79 | name: "command exit status error", 80 | cmdFn: func() error { 81 | cmd := commandFactory.Create("bash", []string{"../command/testdata/exit_with_message.sh"}, nil) 82 | return cmd.Run() 83 | }, 84 | wantErr: `command failed with exit status 1 (bash "../command/testdata/exit_with_message.sh"): check the command's output for details`, 85 | wantMsg: `command failed with exit status 1 (bash "../command/testdata/exit_with_message.sh"): 86 | check the command's output for details`, 87 | }, 88 | { 89 | name: "command execution failed, wrapped", 90 | cmdFn: func() error { 91 | cmd := commandFactory.Create("__notfoundinpath", []string{}, nil) 92 | if err := cmd.Run(); err != nil { 93 | return fmt.Errorf("wrapped: %w", err) 94 | } 95 | return nil 96 | }, 97 | wantErr: `wrapped: executing command failed (__notfoundinpath): exec: "__notfoundinpath": executable file not found in $PATH`, 98 | wantMsg: `wrapped: 99 | executing command failed (__notfoundinpath): 100 | exec: "__notfoundinpath": 101 | executable file not found in $PATH`, 102 | }, 103 | { 104 | name: "command error, wrapped", 105 | cmdFn: func() error { 106 | cmd := commandFactory.Create("bash", []string{"../command/testdata/exit_with_message.sh"}, nil) 107 | if err := cmd.Run(); err != nil { 108 | return fmt.Errorf("wrapped: %w", err) 109 | } 110 | return nil 111 | }, 112 | wantErr: `wrapped: command failed with exit status 1 (bash "../command/testdata/exit_with_message.sh"): check the command's output for details`, 113 | wantMsg: `wrapped: 114 | command failed with exit status 1 (bash "../command/testdata/exit_with_message.sh"): 115 | check the command's output for details`, 116 | }, 117 | { 118 | name: "command with error finder", 119 | cmdFn: func() error { 120 | errorFinder := func(out string) []string { 121 | var errors []string 122 | for _, line := range strings.Split(out, "\n") { 123 | if strings.Contains(line, "Error:") { 124 | errors = append(errors, line) 125 | } 126 | } 127 | return errors 128 | } 129 | 130 | cmd := commandFactory.Create("bash", []string{"../command/testdata/exit_with_message.sh"}, &command.Opts{ 131 | ErrorFinder: errorFinder, 132 | }) 133 | 134 | err := cmd.Run() 135 | return err 136 | }, 137 | wantErr: `command failed with exit status 1 (bash "../command/testdata/exit_with_message.sh"): Error: first error 138 | Error: second error 139 | Error: third error 140 | Error: fourth error`, 141 | wantMsg: `command failed with exit status 1 (bash "../command/testdata/exit_with_message.sh"): 142 | Error: first error 143 | Error: second error 144 | Error: third error 145 | Error: fourth error`, 146 | }, 147 | } 148 | for _, tt := range tests { 149 | t.Run(tt.name, func(t *testing.T) { 150 | err := tt.cmdFn() 151 | 152 | var gotErrMsg string 153 | if err != nil { 154 | gotErrMsg = err.Error() 155 | } 156 | if gotErrMsg != tt.wantErr { 157 | t.Errorf("command.Run() error = \n%v\n, wantErr \n%v\n", gotErrMsg, tt.wantErr) 158 | return 159 | } 160 | 161 | gotFormattedMsg := FormattedError(err) 162 | require.Equal(t, tt.wantMsg, gotFormattedMsg, "FormattedError() error = \n%v\n, wantErr \n%v\n", gotFormattedMsg, tt.wantErr) 163 | if gotFormattedMsg != tt.wantMsg { 164 | t.Errorf("FormattedError() error = \n%v\n, wantErr \n%v\n", gotFormattedMsg, tt.wantErr) 165 | } 166 | }) 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /exitcode/exitcode.go: -------------------------------------------------------------------------------- 1 | //go:generate stringer -type=ExitCode 2 | 3 | package exitcode 4 | 5 | // ExitCode is a simple integer type that represents an exit code. 6 | // It can be used to provide a more semantically meaningful exit code than a simple integer. 7 | type ExitCode int 8 | 9 | const ( 10 | // Success indicates that the program exited successfully. 11 | Success ExitCode = 0 12 | 13 | // Failure indicates that the program exited unsuccessfully. 14 | Failure ExitCode = 1 15 | ) 16 | -------------------------------------------------------------------------------- /exitcode/exitcode_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -type=ExitCode"; DO NOT EDIT. 2 | 3 | package exitcode 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[Success-0] 12 | _ = x[Failure-1] 13 | } 14 | 15 | const _ExitCode_name = "SuccessFailure" 16 | 17 | var _ExitCode_index = [...]uint8{0, 7, 14} 18 | 19 | func (i ExitCode) String() string { 20 | if i < 0 || i >= ExitCode(len(_ExitCode_index)-1) { 21 | return "ExitCode(" + strconv.FormatInt(int64(i), 10) + ")" 22 | } 23 | return _ExitCode_name[_ExitCode_index[i]:_ExitCode_index[i+1]] 24 | } 25 | -------------------------------------------------------------------------------- /fileutil/fileutil.go: -------------------------------------------------------------------------------- 1 | package fileutil 2 | 3 | import ( 4 | "errors" 5 | "io" 6 | "io/fs" 7 | "os" 8 | "path/filepath" 9 | ) 10 | 11 | // FileManager ... 12 | type FileManager interface { 13 | Open(path string) (*os.File, error) 14 | OpenReaderIfExists(path string) (io.Reader, error) 15 | ReadDirEntryNames(path string) ([]string, error) 16 | Remove(path string) error 17 | RemoveAll(path string) error 18 | Write(path string, value string, perm os.FileMode) error 19 | WriteBytes(path string, value []byte) error 20 | FileSizeInBytes(pth string) (int64, error) 21 | } 22 | 23 | type fileManager struct { 24 | } 25 | 26 | // NewFileManager ... 27 | func NewFileManager() FileManager { 28 | return fileManager{} 29 | } 30 | 31 | // ReadDirEntryNames reads the named directory using os.ReadDir and returns the dir entries' names. 32 | func (fileManager) ReadDirEntryNames(path string) ([]string, error) { 33 | entries, err := os.ReadDir(path) 34 | if err != nil { 35 | return nil, err 36 | } 37 | var names []string 38 | for _, entry := range entries { 39 | names = append(names, entry.Name()) 40 | } 41 | return names, nil 42 | } 43 | 44 | // Open ... 45 | func (fileManager) Open(path string) (*os.File, error) { 46 | return os.Open(path) 47 | } 48 | 49 | // OpenReaderIfExists opens the named file using os.Open and returns an io.Reader. 50 | // An ErrNotExist error is absorbed and the returned io.Reader will be nil, 51 | // other errors from os.Open are returned as is. 52 | func (fileManager) OpenReaderIfExists(path string) (io.Reader, error) { 53 | file, err := os.Open(path) 54 | if errors.Is(err, fs.ErrNotExist) { 55 | return nil, nil 56 | } 57 | if err != nil { 58 | return nil, err 59 | } 60 | return file, nil 61 | } 62 | 63 | // Remove ... 64 | func (fileManager) Remove(path string) error { 65 | return os.Remove(path) 66 | } 67 | 68 | // RemoveAll ... 69 | func (fileManager) RemoveAll(path string) error { 70 | return os.RemoveAll(path) 71 | } 72 | 73 | // Write ... 74 | func (f fileManager) Write(path string, value string, mode os.FileMode) error { 75 | if err := f.ensureSavePath(path); err != nil { 76 | return err 77 | } 78 | if err := os.WriteFile(path, []byte(value), mode); err != nil { 79 | return err 80 | } 81 | return os.Chmod(path, mode) 82 | } 83 | 84 | func (fileManager) ensureSavePath(savePath string) error { 85 | dirPath := filepath.Dir(savePath) 86 | return os.MkdirAll(dirPath, 0700) 87 | } 88 | 89 | // WriteBytes ... 90 | func (f fileManager) WriteBytes(path string, value []byte) error { 91 | return os.WriteFile(path, value, 0600) 92 | } 93 | 94 | // FileSizeInBytes checks if the provided path exists and return with the file size (bytes) using os.Lstat. 95 | func (fileManager) FileSizeInBytes(pth string) (int64, error) { 96 | if pth == "" { 97 | return 0, errors.New("No path provided") 98 | } 99 | fileInf, err := os.Stat(pth) 100 | if err != nil { 101 | return 0, err 102 | } 103 | 104 | return fileInf.Size(), nil 105 | } 106 | -------------------------------------------------------------------------------- /fileutil/fileutil_test.go: -------------------------------------------------------------------------------- 1 | package fileutil 2 | 3 | import ( 4 | "os" 5 | "path/filepath" 6 | "testing" 7 | 8 | "github.com/bitrise-io/go-utils/v2/pathutil" 9 | "github.com/stretchr/testify/require" 10 | ) 11 | 12 | func TestWrite(t *testing.T) { 13 | provider := pathutil.NewPathProvider() 14 | tmpDirPath, err := provider.CreateTempDir("go-utils-test-") 15 | require.NoError(t, err) 16 | manager := NewFileManager() 17 | 18 | t.Log("Success when writing string to existing dir") 19 | const content = "test string" 20 | { 21 | tmpFilePath := filepath.Join(tmpDirPath, "WriteStringToFile-success.txt") 22 | require.NoError(t, manager.Write(tmpFilePath, content, 0600)) 23 | 24 | fileContent, err := os.ReadFile(tmpFilePath) 25 | require.NoError(t, err) 26 | require.Equal(t, content, string(fileContent)) 27 | } 28 | 29 | t.Log("Success when writing string to non-existing dir") 30 | { 31 | tmpFilePath := filepath.Join(tmpDirPath, "dir-does-not-exist", "WriteStringToFile-success.txt") 32 | require.NoError(t, manager.Write(tmpFilePath, content, 0600)) 33 | 34 | fileContent, err := os.ReadFile(tmpFilePath) 35 | require.NoError(t, err) 36 | require.Equal(t, content, string(fileContent)) 37 | } 38 | t.Log("Success when writing bytes to existing dir") 39 | 40 | { 41 | tmpFilePath := filepath.Join(tmpDirPath, "WriteBytesToFile-success.txt") 42 | require.NoError(t, manager.WriteBytes(tmpFilePath, []byte("test string"))) 43 | 44 | fileContent, err := os.ReadFile(tmpFilePath) 45 | require.NoError(t, err) 46 | require.Equal(t, "test string", string(fileContent)) 47 | } 48 | 49 | t.Log("Failure when writing bytes to non-existing dir") 50 | { 51 | tmpFilePath := filepath.Join(tmpDirPath, "dir-does-not-exist-2", "WriteBytesToFile-error.txt") 52 | require.Error(t, manager.WriteBytes(tmpFilePath, []byte("test string")), "open "+tmpFilePath+": no such file or directory") 53 | } 54 | } 55 | 56 | func TestFileSizeInBytes(t *testing.T) { 57 | provider := pathutil.NewPathProvider() 58 | tmpDirPath, err := provider.CreateTempDir("go-utils-test-") 59 | require.NoError(t, err) 60 | manager := NewFileManager() 61 | t.Run("Success when existing file provided", func(t *testing.T) { 62 | const content = "test string" 63 | { 64 | tmpFilePath := filepath.Join(tmpDirPath, "FileSizeInBytes-success.txt") 65 | require.NoError(t, manager.Write(tmpFilePath, content, 0600)) 66 | 67 | fileSize, err := manager.FileSizeInBytes(tmpFilePath) 68 | require.NoError(t, err) 69 | require.Equal(t, int64(len([]byte(content))), fileSize) 70 | } 71 | }) 72 | 73 | t.Run("Failure when non-existing path", func(t *testing.T) { 74 | tmpFilePath := filepath.Join(tmpDirPath, "dir-does-not-exist-2", "FileSizeInBytes-error.txt") 75 | _, err := manager.FileSizeInBytes(tmpFilePath) 76 | require.EqualError(t, err, "stat "+tmpFilePath+": no such file or directory") 77 | }) 78 | 79 | t.Run("Failure when path is empty string", func(t *testing.T) { 80 | _, err := manager.FileSizeInBytes("") 81 | require.EqualError(t, err, "No path provided") 82 | }) 83 | } 84 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/bitrise-io/go-utils/v2 2 | 3 | go 1.17 4 | 5 | require ( 6 | github.com/gofrs/uuid/v5 v5.2.0 7 | github.com/hashicorp/go-retryablehttp v0.7.7 8 | github.com/mitchellh/mapstructure v1.5.0 9 | github.com/stretchr/testify v1.9.0 10 | golang.org/x/sys v0.22.0 11 | ) 12 | 13 | require ( 14 | github.com/davecgh/go-spew v1.1.1 // indirect 15 | github.com/hashicorp/go-cleanhttp v0.5.2 // indirect 16 | github.com/pmezard/go-difflib v1.0.0 // indirect 17 | github.com/stretchr/objx v0.5.2 // indirect 18 | gopkg.in/yaml.v3 v3.0.1 // indirect 19 | ) 20 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= 5 | github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= 6 | github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= 7 | github.com/gofrs/uuid/v5 v5.2.0 h1:qw1GMx6/y8vhVsx626ImfKMuS5CvJmhIKKtuyvfajMM= 8 | github.com/gofrs/uuid/v5 v5.2.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= 9 | github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= 10 | github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= 11 | github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= 12 | github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= 13 | github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU= 14 | github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk= 15 | github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= 16 | github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= 17 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 18 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 19 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 20 | github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= 21 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 22 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 23 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 24 | github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= 25 | github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 26 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 27 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 28 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 29 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 30 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 31 | github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= 32 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 33 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 34 | github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= 35 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 36 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 37 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 38 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 39 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 40 | golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 41 | golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 42 | golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 43 | golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 44 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 45 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 46 | golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 47 | golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 48 | golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= 49 | golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 50 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 51 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 52 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 53 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 54 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 55 | -------------------------------------------------------------------------------- /log/colorstring/colorstring.go: -------------------------------------------------------------------------------- 1 | package colorstring 2 | 3 | // ANSI color escape sequences 4 | 5 | import ( 6 | "fmt" 7 | ) 8 | 9 | // Color ... 10 | type Color string 11 | 12 | const ( 13 | blackColor Color = "\x1b[30;1m" 14 | redColor Color = "\x1b[31;1m" 15 | greenColor Color = "\x1b[32;1m" 16 | yellowColor Color = "\x1b[33;1m" 17 | blueColor Color = "\x1b[34;1m" 18 | magentaColor Color = "\x1b[35;1m" 19 | cyanColor Color = "\x1b[36;1m" 20 | resetColor Color = "\x1b[0m" 21 | ) 22 | 23 | // ColorFunc ... 24 | type ColorFunc func(a ...interface{}) string 25 | 26 | func addColor(color Color, msg string) string { 27 | return string(color) + msg + string(resetColor) 28 | } 29 | 30 | // NoColor ... 31 | func NoColor(a ...interface{}) string { 32 | return fmt.Sprint(a...) 33 | } 34 | 35 | // Black ... 36 | func Black(a ...interface{}) string { 37 | return addColor(blackColor, fmt.Sprint(a...)) 38 | } 39 | 40 | // Red ... 41 | func Red(a ...interface{}) string { 42 | return addColor(redColor, fmt.Sprint(a...)) 43 | } 44 | 45 | // Green ... 46 | func Green(a ...interface{}) string { 47 | return addColor(greenColor, fmt.Sprint(a...)) 48 | } 49 | 50 | // Yellow ... 51 | func Yellow(a ...interface{}) string { 52 | return addColor(yellowColor, fmt.Sprint(a...)) 53 | } 54 | 55 | // Blue ... 56 | func Blue(a ...interface{}) string { 57 | return addColor(blueColor, fmt.Sprint(a...)) 58 | } 59 | 60 | // Magenta ... 61 | func Magenta(a ...interface{}) string { 62 | return addColor(magentaColor, fmt.Sprint(a...)) 63 | } 64 | 65 | // Cyan ... 66 | func Cyan(a ...interface{}) string { 67 | return addColor(cyanColor, fmt.Sprint(a...)) 68 | } 69 | 70 | // ColorfFunc ... 71 | type ColorfFunc func(format string, a ...interface{}) string 72 | 73 | // NoColorf ... 74 | func NoColorf(format string, a ...interface{}) string { 75 | return NoColor(fmt.Sprintf(format, a...)) 76 | } 77 | 78 | // Blackf ... 79 | func Blackf(format string, a ...interface{}) string { 80 | return Black(fmt.Sprintf(format, a...)) 81 | } 82 | 83 | // Redf ... 84 | func Redf(format string, a ...interface{}) string { 85 | return Red(fmt.Sprintf(format, a...)) 86 | } 87 | 88 | // Greenf ... 89 | func Greenf(format string, a ...interface{}) string { 90 | return Green(fmt.Sprintf(format, a...)) 91 | } 92 | 93 | // Yellowf ... 94 | func Yellowf(format string, a ...interface{}) string { 95 | return Yellow(fmt.Sprintf(format, a...)) 96 | } 97 | 98 | // Bluef ... 99 | func Bluef(format string, a ...interface{}) string { 100 | return Blue(fmt.Sprintf(format, a...)) 101 | } 102 | 103 | // Magentaf ... 104 | func Magentaf(format string, a ...interface{}) string { 105 | return Magenta(fmt.Sprintf(format, a...)) 106 | } 107 | 108 | // Cyanf ... 109 | func Cyanf(format string, a ...interface{}) string { 110 | return Cyan(fmt.Sprintf(format, a...)) 111 | } 112 | -------------------------------------------------------------------------------- /log/colorstring/colorstring_test.go: -------------------------------------------------------------------------------- 1 | package colorstring 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/require" 8 | ) 9 | 10 | func TestAddColor(t *testing.T) { 11 | /* 12 | blackColor Color = "\x1b[30;1m" 13 | resetColor Color = "\x1b[0m" 14 | */ 15 | 16 | t.Log("colored_string = color + string + reset_color") 17 | { 18 | desiredColored := "\x1b[30;1m" + "test" + "\x1b[0m" 19 | colored := addColor(blackColor, "test") 20 | require.Equal(t, desiredColored, colored) 21 | } 22 | } 23 | 24 | func TestBlack(t *testing.T) { 25 | t.Log("Simple string can be blacked") 26 | { 27 | desiredColored := "\x1b[30;1m" + "test" + "\x1b[0m" 28 | colored := Black("test") 29 | require.Equal(t, desiredColored, colored) 30 | } 31 | 32 | t.Log("Multiple strings can be blacked") 33 | { 34 | desiredColored := "\x1b[30;1m" + "Hello Bitrise !" + "\x1b[0m" 35 | colored := Black("Hello ", "Bitrise ", "!") 36 | require.Equal(t, desiredColored, colored) 37 | } 38 | } 39 | 40 | func TestBlackf(t *testing.T) { 41 | t.Log("Simple format can be blacked") 42 | { 43 | desiredColored := "\x1b[30;1m" + fmt.Sprintf("Hello %s", "bitrise") + "\x1b[0m" 44 | colored := Blackf("Hello %s", "bitrise") 45 | require.Equal(t, desiredColored, colored) 46 | } 47 | 48 | t.Log("Complex format can be blacked") 49 | { 50 | desiredColored := "\x1b[30;1m" + fmt.Sprintf("Hello %s %s", "bitrise", "!") + "\x1b[0m" 51 | colored := Blackf("Hello %s %s", "bitrise", "!") 52 | require.Equal(t, desiredColored, colored) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /log/log.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | "time" 8 | ) 9 | 10 | // Logger ... 11 | type Logger interface { 12 | Infof(format string, v ...interface{}) 13 | Warnf(format string, v ...interface{}) 14 | Printf(format string, v ...interface{}) 15 | Donef(format string, v ...interface{}) 16 | Debugf(format string, v ...interface{}) 17 | Errorf(format string, v ...interface{}) 18 | TInfof(format string, v ...interface{}) 19 | TWarnf(format string, v ...interface{}) 20 | TPrintf(format string, v ...interface{}) 21 | TDonef(format string, v ...interface{}) 22 | TDebugf(format string, v ...interface{}) 23 | TErrorf(format string, v ...interface{}) 24 | Println() 25 | EnableDebugLog(enable bool) 26 | } 27 | 28 | const defaultTimeStampLayout = "15:04:05" 29 | 30 | type logger struct { 31 | enableDebugLog bool 32 | timestampLayout string 33 | stdout io.Writer 34 | } 35 | 36 | // NewLogger ... 37 | func NewLogger() Logger { 38 | return &logger{enableDebugLog: false, timestampLayout: defaultTimeStampLayout, stdout: os.Stdout} 39 | } 40 | 41 | // EnableDebugLog ... 42 | func (l *logger) EnableDebugLog(enable bool) { 43 | l.enableDebugLog = enable 44 | } 45 | 46 | // Infof ... 47 | func (l *logger) Infof(format string, v ...interface{}) { 48 | l.printf(infoSeverity, false, format, v...) 49 | } 50 | 51 | // Warnf ... 52 | func (l *logger) Warnf(format string, v ...interface{}) { 53 | l.printf(warnSeverity, false, format, v...) 54 | } 55 | 56 | // Printf ... 57 | func (l *logger) Printf(format string, v ...interface{}) { 58 | l.printf(normalSeverity, false, format, v...) 59 | } 60 | 61 | // Donef ... 62 | func (l *logger) Donef(format string, v ...interface{}) { 63 | l.printf(doneSeverity, false, format, v...) 64 | } 65 | 66 | // Debugf ... 67 | func (l *logger) Debugf(format string, v ...interface{}) { 68 | if l.enableDebugLog { 69 | l.printf(debugSeverity, false, format, v...) 70 | } 71 | } 72 | 73 | // Errorf ... 74 | func (l *logger) Errorf(format string, v ...interface{}) { 75 | l.printf(errorSeverity, false, format, v...) 76 | } 77 | 78 | // TInfof ... 79 | func (l *logger) TInfof(format string, v ...interface{}) { 80 | l.printf(infoSeverity, true, format, v...) 81 | } 82 | 83 | // TWarnf ... 84 | func (l *logger) TWarnf(format string, v ...interface{}) { 85 | l.printf(warnSeverity, true, format, v...) 86 | } 87 | 88 | // TPrintf ... 89 | func (l *logger) TPrintf(format string, v ...interface{}) { 90 | l.printf(normalSeverity, true, format, v...) 91 | } 92 | 93 | // TDonef ... 94 | func (l *logger) TDonef(format string, v ...interface{}) { 95 | l.printf(doneSeverity, true, format, v...) 96 | } 97 | 98 | // TDebugf ... 99 | func (l *logger) TDebugf(format string, v ...interface{}) { 100 | if l.enableDebugLog { 101 | l.printf(debugSeverity, true, format, v...) 102 | } 103 | } 104 | 105 | // TErrorf ... 106 | func (l *logger) TErrorf(format string, v ...interface{}) { 107 | l.printf(errorSeverity, true, format, v...) 108 | } 109 | 110 | // Println ... 111 | func (l *logger) Println() { 112 | fmt.Println() 113 | } 114 | 115 | func (l *logger) timestampField() string { 116 | currentTime := time.Now() 117 | return fmt.Sprintf("[%s]", currentTime.Format(l.timestampLayout)) 118 | } 119 | 120 | func (l *logger) prefixCurrentTime(message string) string { 121 | return fmt.Sprintf("%s %s", l.timestampField(), message) 122 | } 123 | 124 | func (l *logger) createLogMsg(severity Severity, withTime bool, format string, v ...interface{}) string { 125 | colorFunc := severityColorFuncMap[severity] 126 | message := colorFunc(format, v...) 127 | if withTime { 128 | message = l.prefixCurrentTime(message) 129 | } 130 | 131 | return message 132 | } 133 | 134 | func (l *logger) printf(severity Severity, withTime bool, format string, v ...interface{}) { 135 | message := l.createLogMsg(severity, withTime, format, v...) 136 | if _, err := fmt.Fprintln(l.stdout, message); err != nil { 137 | fmt.Printf("failed to print message: %s, error: %s\n", message, err) 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /log/log_test.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "bytes" 5 | "regexp" 6 | "testing" 7 | 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | func TestSetOutWriter(t *testing.T) { 12 | var b bytes.Buffer 13 | logger := logger{ 14 | stdout: &b, 15 | } 16 | logger.Printf("test %s", "log") 17 | require.Equal(t, "test log\n", b.String()) 18 | } 19 | 20 | func TestSetEnableDebugLog(t *testing.T) { 21 | t.Log("enable debug log") 22 | { 23 | var b bytes.Buffer 24 | logger := logger{ 25 | enableDebugLog: true, 26 | stdout: &b, 27 | } 28 | logger.Debugf("test %s", "log") 29 | require.Equal(t, "\x1b[35;1mtest log\x1b[0m\n", b.String()) 30 | } 31 | 32 | t.Log("disable debug log") 33 | { 34 | var b bytes.Buffer 35 | logger := logger{ 36 | enableDebugLog: false, 37 | stdout: &b, 38 | } 39 | logger.Debugf("test %s", "log") 40 | require.Equal(t, "", b.String()) 41 | } 42 | } 43 | 44 | func TestSetTimestampLayout(t *testing.T) { 45 | var b bytes.Buffer 46 | logger := logger{ 47 | timestampLayout: "15-04-05", 48 | stdout: &b, 49 | } 50 | logger.TPrintf("test %s", "log") 51 | re := regexp.MustCompile(`\[.+-.+-.+] test log`) 52 | require.True(t, re.MatchString(b.String()), b.String()) 53 | } 54 | 55 | func Test_printf_with_time(t *testing.T) { 56 | var b bytes.Buffer 57 | logger := logger{ 58 | enableDebugLog: false, 59 | timestampLayout: "15.04.05", 60 | stdout: &b, 61 | } 62 | logger.TPrintf("test %s", "log") 63 | re := regexp.MustCompile(`\[.+\..+\..+] test log`) 64 | require.True(t, re.MatchString(b.String()), b.String()) 65 | } 66 | 67 | func Test_printf_severity(t *testing.T) { 68 | t.Log("error") 69 | { 70 | var b bytes.Buffer 71 | logger := logger{ 72 | enableDebugLog: false, 73 | timestampLayout: "", 74 | stdout: &b, 75 | } 76 | logger.Errorf("test %s", "log") 77 | require.Equal(t, "\x1b[31;1mtest log\x1b[0m\n", b.String()) 78 | } 79 | 80 | t.Log("warn") 81 | { 82 | var b bytes.Buffer 83 | logger := logger{ 84 | enableDebugLog: false, 85 | timestampLayout: "", 86 | stdout: &b, 87 | } 88 | logger.Warnf("test %s", "log") 89 | require.Equal(t, "\x1b[33;1mtest log\x1b[0m\n", b.String()) 90 | } 91 | 92 | t.Log("debug") 93 | { 94 | var b bytes.Buffer 95 | logger := logger{ 96 | enableDebugLog: true, 97 | timestampLayout: "", 98 | stdout: &b, 99 | } 100 | logger.Debugf("test %s", "log") 101 | require.Equal(t, "\x1b[35;1mtest log\x1b[0m\n", b.String()) 102 | } 103 | 104 | t.Log("normal") 105 | { 106 | var b bytes.Buffer 107 | logger := logger{ 108 | enableDebugLog: false, 109 | timestampLayout: "", 110 | stdout: &b, 111 | } 112 | logger.Printf("test %s", "log") 113 | require.Equal(t, "test log\n", b.String()) 114 | } 115 | 116 | t.Log("info") 117 | { 118 | var b bytes.Buffer 119 | logger := logger{ 120 | enableDebugLog: false, 121 | timestampLayout: "", 122 | stdout: &b, 123 | } 124 | logger.Infof("test %s", "log") 125 | require.Equal(t, "\x1b[34;1mtest log\x1b[0m\n", b.String()) 126 | } 127 | 128 | t.Log("success") 129 | { 130 | var b bytes.Buffer 131 | logger := logger{ 132 | enableDebugLog: false, 133 | timestampLayout: "", 134 | stdout: &b, 135 | } 136 | logger.Donef("test %s", "log") 137 | require.Equal(t, "\x1b[32;1mtest log\x1b[0m\n", b.String()) 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /log/severity.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import "github.com/bitrise-io/go-utils/v2/log/colorstring" 4 | 5 | // Severity ... 6 | type Severity uint8 7 | 8 | const ( 9 | errorSeverity Severity = iota 10 | warnSeverity 11 | normalSeverity 12 | infoSeverity 13 | doneSeverity 14 | debugSeverity 15 | ) 16 | 17 | type severityColorFunc colorstring.ColorfFunc 18 | 19 | var ( 20 | doneSeverityColorFunc severityColorFunc = colorstring.Greenf 21 | infoSeverityColorFunc severityColorFunc = colorstring.Bluef 22 | normalSeverityColorFunc severityColorFunc = colorstring.NoColorf 23 | debugSeverityColorFunc severityColorFunc = colorstring.Magentaf 24 | warnSeverityColorFunc severityColorFunc = colorstring.Yellowf 25 | errorSeverityColorFunc severityColorFunc = colorstring.Redf 26 | ) 27 | 28 | var severityColorFuncMap = map[Severity]severityColorFunc{ 29 | doneSeverity: doneSeverityColorFunc, 30 | infoSeverity: infoSeverityColorFunc, 31 | normalSeverity: normalSeverityColorFunc, 32 | debugSeverity: debugSeverityColorFunc, 33 | warnSeverity: warnSeverityColorFunc, 34 | errorSeverity: errorSeverityColorFunc, 35 | } 36 | -------------------------------------------------------------------------------- /mocks/Client.go: -------------------------------------------------------------------------------- 1 | // Code generated by mockery v2.9.4. DO NOT EDIT. 2 | 3 | package mocks 4 | 5 | import ( 6 | bytes "bytes" 7 | 8 | mock "github.com/stretchr/testify/mock" 9 | ) 10 | 11 | // Client is an autogenerated mock type for the Client type 12 | type Client struct { 13 | mock.Mock 14 | } 15 | 16 | // Send provides a mock function with given fields: buffer 17 | func (_m *Client) Send(buffer *bytes.Buffer) { 18 | _m.Called(buffer) 19 | } 20 | -------------------------------------------------------------------------------- /mocks/Logger.go: -------------------------------------------------------------------------------- 1 | // Code generated by mockery v2.9.4. DO NOT EDIT. 2 | 3 | package mocks 4 | 5 | import mock "github.com/stretchr/testify/mock" 6 | 7 | // Logger is an autogenerated mock type for the Logger type 8 | type Logger struct { 9 | mock.Mock 10 | } 11 | 12 | // Debugf provides a mock function with given fields: format, v 13 | func (_m *Logger) Debugf(format string, v ...interface{}) { 14 | var _ca []interface{} 15 | _ca = append(_ca, format) 16 | _ca = append(_ca, v...) 17 | _m.Called(_ca...) 18 | } 19 | 20 | // Donef provides a mock function with given fields: format, v 21 | func (_m *Logger) Donef(format string, v ...interface{}) { 22 | var _ca []interface{} 23 | _ca = append(_ca, format) 24 | _ca = append(_ca, v...) 25 | _m.Called(_ca...) 26 | } 27 | 28 | // EnableDebugLog provides a mock function with given fields: enable 29 | func (_m *Logger) EnableDebugLog(enable bool) { 30 | _m.Called(enable) 31 | } 32 | 33 | // Errorf provides a mock function with given fields: format, v 34 | func (_m *Logger) Errorf(format string, v ...interface{}) { 35 | var _ca []interface{} 36 | _ca = append(_ca, format) 37 | _ca = append(_ca, v...) 38 | _m.Called(_ca...) 39 | } 40 | 41 | // Infof provides a mock function with given fields: format, v 42 | func (_m *Logger) Infof(format string, v ...interface{}) { 43 | var _ca []interface{} 44 | _ca = append(_ca, format) 45 | _ca = append(_ca, v...) 46 | _m.Called(_ca...) 47 | } 48 | 49 | // Printf provides a mock function with given fields: format, v 50 | func (_m *Logger) Printf(format string, v ...interface{}) { 51 | var _ca []interface{} 52 | _ca = append(_ca, format) 53 | _ca = append(_ca, v...) 54 | _m.Called(_ca...) 55 | } 56 | 57 | // Println provides a mock function with given fields: 58 | func (_m *Logger) Println() { 59 | _m.Called() 60 | } 61 | 62 | // TDebugf provides a mock function with given fields: format, v 63 | func (_m *Logger) TDebugf(format string, v ...interface{}) { 64 | var _ca []interface{} 65 | _ca = append(_ca, format) 66 | _ca = append(_ca, v...) 67 | _m.Called(_ca...) 68 | } 69 | 70 | // TDonef provides a mock function with given fields: format, v 71 | func (_m *Logger) TDonef(format string, v ...interface{}) { 72 | var _ca []interface{} 73 | _ca = append(_ca, format) 74 | _ca = append(_ca, v...) 75 | _m.Called(_ca...) 76 | } 77 | 78 | // TErrorf provides a mock function with given fields: format, v 79 | func (_m *Logger) TErrorf(format string, v ...interface{}) { 80 | var _ca []interface{} 81 | _ca = append(_ca, format) 82 | _ca = append(_ca, v...) 83 | _m.Called(_ca...) 84 | } 85 | 86 | // TInfof provides a mock function with given fields: format, v 87 | func (_m *Logger) TInfof(format string, v ...interface{}) { 88 | var _ca []interface{} 89 | _ca = append(_ca, format) 90 | _ca = append(_ca, v...) 91 | _m.Called(_ca...) 92 | } 93 | 94 | // TPrintf provides a mock function with given fields: format, v 95 | func (_m *Logger) TPrintf(format string, v ...interface{}) { 96 | var _ca []interface{} 97 | _ca = append(_ca, format) 98 | _ca = append(_ca, v...) 99 | _m.Called(_ca...) 100 | } 101 | 102 | // TWarnf provides a mock function with given fields: format, v 103 | func (_m *Logger) TWarnf(format string, v ...interface{}) { 104 | var _ca []interface{} 105 | _ca = append(_ca, format) 106 | _ca = append(_ca, v...) 107 | _m.Called(_ca...) 108 | } 109 | 110 | // Warnf provides a mock function with given fields: format, v 111 | func (_m *Logger) Warnf(format string, v ...interface{}) { 112 | var _ca []interface{} 113 | _ca = append(_ca, format) 114 | _ca = append(_ca, v...) 115 | _m.Called(_ca...) 116 | } 117 | -------------------------------------------------------------------------------- /pathutil/pathutil.go: -------------------------------------------------------------------------------- 1 | package pathutil 2 | 3 | import ( 4 | "errors" 5 | "os" 6 | "os/user" 7 | "path/filepath" 8 | "strings" 9 | ) 10 | 11 | // PathProvider ... 12 | type PathProvider interface { 13 | CreateTempDir(prefix string) (string, error) 14 | } 15 | 16 | type pathProvider struct{} 17 | 18 | // NewPathProvider ... 19 | func NewPathProvider() PathProvider { 20 | return pathProvider{} 21 | } 22 | 23 | // CreateTempDir creates a temp dir, and returns its path. 24 | // If prefix is provided it'll be used as the tmp dir's name prefix. 25 | // Normalized: it's guaranteed that the path won't end with '/'. 26 | func (pathProvider) CreateTempDir(prefix string) (dir string, err error) { 27 | dir, err = os.MkdirTemp("", prefix) 28 | dir = strings.TrimSuffix(dir, "/") 29 | 30 | return 31 | } 32 | 33 | // PathChecker ... 34 | type PathChecker interface { 35 | IsPathExists(pth string) (bool, error) 36 | IsDirExists(pth string) (bool, error) 37 | } 38 | 39 | type pathChecker struct{} 40 | 41 | // NewPathChecker ... 42 | func NewPathChecker() PathChecker { 43 | return pathChecker{} 44 | } 45 | 46 | // IsPathExists ... 47 | func (c pathChecker) IsPathExists(pth string) (bool, error) { 48 | _, isExists, err := c.genericIsPathExists(pth) 49 | return isExists, err 50 | } 51 | 52 | // IsDirExists ... 53 | func (c pathChecker) IsDirExists(pth string) (bool, error) { 54 | info, isExists, err := c.genericIsPathExists(pth) 55 | return isExists && info.IsDir(), err 56 | } 57 | 58 | func (pathChecker) genericIsPathExists(pth string) (os.FileInfo, bool, error) { 59 | if pth == "" { 60 | return nil, false, errors.New("no path provided") 61 | } 62 | 63 | fileInf, err := os.Lstat(pth) 64 | if err == nil { 65 | return fileInf, true, nil 66 | } 67 | 68 | if os.IsNotExist(err) { 69 | return nil, false, nil 70 | } 71 | 72 | return fileInf, false, err 73 | } 74 | 75 | // PathModifier ... 76 | type PathModifier interface { 77 | AbsPath(pth string) (string, error) 78 | } 79 | 80 | type pathModifier struct{} 81 | 82 | // NewPathModifier ... 83 | func NewPathModifier() PathModifier { 84 | return pathModifier{} 85 | } 86 | 87 | // AbsPath expands ENV vars and the ~ character then calls Go's Abs 88 | func (p pathModifier) AbsPath(pth string) (string, error) { 89 | if pth == "" { 90 | return "", errors.New("No Path provided") 91 | } 92 | 93 | pth, err := p.expandTilde(pth) 94 | if err != nil { 95 | return "", err 96 | } 97 | 98 | return filepath.Abs(os.ExpandEnv(pth)) 99 | } 100 | 101 | func (pathModifier) expandTilde(pth string) (string, error) { 102 | if pth == "" { 103 | return "", errors.New("No Path provided") 104 | } 105 | 106 | if strings.HasPrefix(pth, "~") { 107 | pth = strings.TrimPrefix(pth, "~") 108 | 109 | if len(pth) == 0 || strings.HasPrefix(pth, "/") { 110 | return os.ExpandEnv("$HOME" + pth), nil 111 | } 112 | 113 | splitPth := strings.Split(pth, "/") 114 | username := splitPth[0] 115 | 116 | usr, err := user.Lookup(username) 117 | if err != nil { 118 | return "", err 119 | } 120 | 121 | pathInUsrHome := strings.Join(splitPth[1:], "/") 122 | 123 | return filepath.Join(usr.HomeDir, pathInUsrHome), nil 124 | } 125 | 126 | return pth, nil 127 | } 128 | -------------------------------------------------------------------------------- /pathutil/pathutil_test.go: -------------------------------------------------------------------------------- 1 | package pathutil 2 | 3 | import ( 4 | "os" 5 | "os/user" 6 | "path/filepath" 7 | "strings" 8 | "testing" 9 | 10 | "github.com/stretchr/testify/require" 11 | ) 12 | 13 | func Test_pathProvider_CreateTempDir(t *testing.T) { 14 | tests := []struct { 15 | name string 16 | prefix string 17 | }{ 18 | { 19 | name: "prefix provided", 20 | prefix: "some-test", 21 | }, 22 | { 23 | name: "empty prefix", 24 | prefix: "", 25 | }, 26 | } 27 | for _, tt := range tests { 28 | t.Run(tt.name, func(t *testing.T) { 29 | p := pathProvider{} 30 | gotDir, err := p.CreateTempDir(tt.prefix) 31 | 32 | require.NoError(t, err) 33 | require.True(t, len(gotDir) != 0) 34 | require.True(t, strings.HasPrefix(filepath.Base(gotDir), tt.prefix)) 35 | // returned temp dir path should not have a / at it's end 36 | require.False(t, strings.HasSuffix(gotDir, "/")) 37 | // directory is created 38 | info, err := os.Lstat(gotDir) 39 | require.NoError(t, err) 40 | require.True(t, info.IsDir()) 41 | }) 42 | } 43 | } 44 | 45 | func Test_pathChecker_IsPathExists(t *testing.T) { 46 | tests := []struct { 47 | name string 48 | pth string 49 | want bool 50 | wantErr bool 51 | }{ 52 | { 53 | name: "path does not exists", 54 | pth: filepath.Join("this", "should", "not", "exist"), 55 | want: false, 56 | }, 57 | { 58 | name: "current directory", 59 | pth: ".", 60 | want: true, 61 | }, 62 | { 63 | name: "empty path", 64 | pth: "", 65 | want: false, 66 | wantErr: true, 67 | }, 68 | } 69 | for _, tt := range tests { 70 | t.Run(tt.name, func(t *testing.T) { 71 | c := pathChecker{} 72 | got, err := c.IsPathExists(tt.pth) 73 | 74 | if tt.wantErr { 75 | require.Error(t, err) 76 | } else { 77 | require.NoError(t, err) 78 | } 79 | require.Equal(t, tt.want, got) 80 | }) 81 | } 82 | } 83 | 84 | func Test_pathChecker_IsDirExists(t *testing.T) { 85 | tmpDirPath := t.TempDir() 86 | tmpFilePath := filepath.Join(t.TempDir(), "hello.txt") 87 | require.NoError(t, os.WriteFile(tmpFilePath, []byte("hello"), 0700)) 88 | 89 | tests := []struct { 90 | name string 91 | path string 92 | want bool 93 | wantErr bool 94 | }{ 95 | { 96 | name: "path does not exists", 97 | path: filepath.Join("this", "should", "not", "exist"), 98 | want: false, 99 | }, 100 | { 101 | name: "current dir", 102 | path: ".", 103 | want: true, 104 | }, 105 | { 106 | name: "empty path", 107 | path: "", 108 | want: false, 109 | wantErr: true, 110 | }, 111 | { 112 | name: "existing file", 113 | path: tmpFilePath, 114 | want: false, 115 | }, 116 | { 117 | name: "existing dir", 118 | path: tmpDirPath, 119 | want: true, 120 | }, 121 | } 122 | for _, tt := range tests { 123 | t.Run(tt.name, func(t *testing.T) { 124 | c := pathChecker{} 125 | got, err := c.IsDirExists(tt.path) 126 | 127 | if tt.wantErr { 128 | require.Error(t, err) 129 | } else { 130 | require.NoError(t, err) 131 | } 132 | require.Equal(t, tt.want, got) 133 | }) 134 | } 135 | } 136 | 137 | func Test_pathModifier_AbsPath(t *testing.T) { 138 | currDirPath, err := filepath.Abs(".") 139 | require.NoError(t, err) 140 | require.NotEqual(t, "", currDirPath) 141 | require.NotEqual(t, ".", currDirPath) 142 | 143 | currentUser, err := user.Current() 144 | require.NoError(t, err) 145 | 146 | sep := string(os.PathSeparator) 147 | homePathEnv := filepath.Join(sep, "path", "home", "test-user") 148 | require.Equal(t, nil, os.Setenv("HOME", homePathEnv)) 149 | 150 | testFileRelPathFromHome := filepath.Join("some", "file.ext") 151 | 152 | tests := []struct { 153 | name string 154 | pth string 155 | want string 156 | wantErr bool 157 | }{ 158 | { 159 | name: "Empty path", 160 | pth: "", 161 | want: "", 162 | wantErr: true, 163 | }, 164 | { 165 | name: "Current dir", 166 | pth: ".", 167 | want: currDirPath, 168 | }, 169 | { 170 | name: "Relative dir", 171 | pth: filepath.Join(homePathEnv, "..", "test-user"), 172 | want: homePathEnv, 173 | }, 174 | { 175 | name: "Environment variable", 176 | pth: filepath.Join("$HOME", testFileRelPathFromHome), 177 | want: filepath.Join(homePathEnv, testFileRelPathFromHome), 178 | }, 179 | { 180 | name: "Tilde with path", 181 | pth: filepath.Join("~", testFileRelPathFromHome), 182 | want: filepath.Join(homePathEnv, testFileRelPathFromHome), 183 | }, 184 | { 185 | name: "Tilde with relative path", 186 | pth: "~" + sep + ".." + sep + "test-user", 187 | want: homePathEnv, 188 | }, 189 | { 190 | name: "Tilde with slash suffix", 191 | pth: "~" + sep, 192 | want: homePathEnv, 193 | }, 194 | { 195 | name: "Tilde only", 196 | pth: "~", 197 | want: homePathEnv, 198 | }, 199 | { 200 | name: "Tilde + username", 201 | pth: "~" + currentUser.Name, 202 | want: currentUser.HomeDir, 203 | }, 204 | { 205 | name: "Tilde with nonexistent username", 206 | pth: filepath.Join("~testaccnotexist", "folder"), 207 | wantErr: true, 208 | }, 209 | { 210 | name: "Tilde + username, slash suffix", 211 | pth: "~" + currentUser.Name + sep, 212 | want: currentUser.HomeDir, 213 | }, 214 | { 215 | name: "Tilde + username with path", 216 | pth: filepath.Join("~"+currentUser.Name, "folder"), 217 | want: filepath.Join(currentUser.HomeDir, "folder"), 218 | }, 219 | { 220 | name: "Tilde as directory name", 221 | pth: filepath.Join(sep, "test", "~", "in", "name"), 222 | want: filepath.Join(sep, "test", "~", "in", "name"), 223 | }, 224 | } 225 | for _, tt := range tests { 226 | t.Run(tt.name, func(t *testing.T) { 227 | p := pathModifier{} 228 | got, err := p.AbsPath(tt.pth) 229 | 230 | if (err != nil) != tt.wantErr { 231 | t.Errorf("pathModifier.AbsPath() error = %v, wantErr %v", err, tt.wantErr) 232 | return 233 | } 234 | require.Equal(t, tt.want, got) 235 | }) 236 | } 237 | } 238 | -------------------------------------------------------------------------------- /redactwriter/range.go: -------------------------------------------------------------------------------- 1 | package redactwriter 2 | 3 | import ( 4 | "bytes" 5 | "sort" 6 | ) 7 | 8 | type matchRange struct{ first, last int } 9 | 10 | // allRanges returns every indexes of instance of pattern in b, or nil if pattern is not present in b. 11 | func allRanges(b, pattern []byte) (ranges []matchRange) { 12 | i := 0 13 | for { 14 | sub := b[i:] 15 | idx := bytes.Index(sub, pattern) 16 | if idx == -1 { 17 | return 18 | } 19 | 20 | ranges = append(ranges, matchRange{first: idx + i, last: idx + i + len(pattern)}) 21 | 22 | i += idx + 1 23 | if i > len(b)-1 { 24 | return 25 | } 26 | } 27 | } 28 | 29 | // mergeAllRanges merges every overlapping ranges in r. 30 | func mergeAllRanges(r []matchRange) []matchRange { 31 | sort.Slice(r, func(i, j int) bool { return r[i].first < r[j].first }) 32 | for i := 0; i < len(r)-1; i++ { 33 | for i+1 < len(r) && r[i+1].first <= r[i].last { 34 | if r[i+1].last > r[i].last { 35 | r[i].last = r[i+1].last 36 | } 37 | r = append(r[:i+1], r[i+2:]...) 38 | } 39 | } 40 | return r 41 | } 42 | -------------------------------------------------------------------------------- /redactwriter/range_test.go: -------------------------------------------------------------------------------- 1 | package redactwriter 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/require" 8 | ) 9 | 10 | func TestAllRanges(t *testing.T) { 11 | { 12 | ranges := allRanges([]byte("test"), []byte("t")) 13 | require.Equal(t, []matchRange{{first: 0, last: 1}, {first: 3, last: 4}}, ranges) 14 | } 15 | 16 | { 17 | ranges := allRanges([]byte("test rangetest"), []byte("test")) 18 | require.Equal(t, []matchRange{{first: 0, last: 4}, {first: 10, last: 14}}, ranges) 19 | } 20 | 21 | { 22 | ranges := allRanges([]byte("\n"), []byte("\n")) 23 | require.Equal(t, []matchRange{{first: 0, last: 1}}, ranges) 24 | } 25 | 26 | { 27 | ranges := allRanges([]byte("test\n"), []byte("\n")) 28 | require.Equal(t, []matchRange{{first: 4, last: 5}}, ranges) 29 | } 30 | 31 | { 32 | ranges := allRanges([]byte("\n\ntest\n"), []byte("\n")) 33 | require.Equal(t, []matchRange{{first: 0, last: 1}, {first: 1, last: 2}, {first: 6, last: 7}}, ranges) 34 | } 35 | 36 | { 37 | ranges := allRanges([]byte("\n\ntest\n"), []byte("test\n")) 38 | require.Equal(t, []matchRange{{first: 2, last: 7}}, ranges) 39 | } 40 | } 41 | 42 | func TestMergeAllRanges(t *testing.T) { 43 | var testCases = []struct { 44 | name string 45 | ranges []matchRange 46 | want []matchRange 47 | }{ 48 | { 49 | name: "merges overlapping ranges", 50 | ranges: []matchRange{{0, 2}, {1, 3}}, 51 | want: []matchRange{{0, 3}}, 52 | }, 53 | { 54 | name: "does not merge distinct ranges", 55 | ranges: []matchRange{{0, 2}, {3, 5}}, 56 | want: []matchRange{{0, 2}, {3, 5}}, 57 | }, 58 | { 59 | name: "returns the wider range", 60 | ranges: []matchRange{{0, 2}, {1, 2}}, 61 | want: []matchRange{{0, 2}}, 62 | }, 63 | { 64 | name: "complex test", 65 | ranges: []matchRange{{11, 15}, {0, 2}, {11, 13}, {2, 4}, {6, 9}, {5, 10}}, 66 | want: []matchRange{{0, 4}, {5, 10}, {11, 15}}, 67 | }, 68 | } 69 | for _, tc := range testCases { 70 | t.Run(tc.name, func(t *testing.T) { 71 | if got := mergeAllRanges(tc.ranges); !reflect.DeepEqual(got, tc.want) { 72 | t.Errorf("got %v, want %v", got, tc.want) 73 | } 74 | }) 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /redactwriter/redactwriter.go: -------------------------------------------------------------------------------- 1 | package redactwriter 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "sort" 7 | "strings" 8 | "sync" 9 | "time" 10 | 11 | "github.com/bitrise-io/go-utils/v2/log" 12 | ) 13 | 14 | // RedactStr ... 15 | const RedactStr = "[REDACTED]" 16 | 17 | var newLine = []byte("\n") 18 | 19 | // Writer ... 20 | type Writer struct { 21 | writer io.Writer 22 | secrets [][][]byte 23 | 24 | chunk []byte 25 | store [][]byte 26 | mux sync.Mutex 27 | timer *time.Timer 28 | logger log.Logger 29 | } 30 | 31 | // New ... 32 | func New(secrets []string, target io.Writer, logger log.Logger) *Writer { 33 | extendedSecrets := secrets 34 | // adding transformed secrets with escaped newline characters to ensure that these are also obscured if found in logs 35 | for _, secret := range secrets { 36 | if strings.Contains(secret, "\n") { 37 | extendedSecrets = append(extendedSecrets, strings.ReplaceAll(secret, "\n", `\n`)) 38 | } 39 | } 40 | 41 | return &Writer{ 42 | writer: target, 43 | secrets: secretsByteList(extendedSecrets), 44 | logger: logger, 45 | } 46 | } 47 | 48 | // Write implements io.Writer interface. 49 | // Splits p into lines, the lines are matched against the secrets, 50 | // this determines which lines can be redacted and passed to the next writer (target). 51 | // There might be lines that need to be buffered since they partially match a secret. 52 | // We do not know the last Write call, so Close needs to be called to flush the buffer. 53 | func (w *Writer) Write(p []byte) (int, error) { 54 | if len(p) == 0 { 55 | return 0, nil 56 | } 57 | 58 | w.mux.Lock() 59 | defer w.mux.Unlock() 60 | 61 | // previous bytes may not end with newline 62 | data := append(w.chunk, p...) 63 | 64 | lastLines, chunk := splitAfterNewline(data) 65 | w.chunk = chunk 66 | if len(chunk) > 0 { 67 | // we have remaining bytes, do not swallow them 68 | if w.timer != nil { 69 | w.timer.Stop() 70 | w.timer.C = nil 71 | } 72 | w.timer = time.AfterFunc(100*time.Millisecond, func() { 73 | if _, err := w.flush(); err != nil { 74 | w.logger.Errorf("Failed to print last lines: %s", err) 75 | } 76 | }) 77 | } 78 | 79 | if len(lastLines) == 0 { 80 | // it is necessary to return the count of incoming bytes 81 | return len(p), nil 82 | } 83 | 84 | for _, line := range lastLines { 85 | lines := append(w.store, line) 86 | matchMap, partialMatchIndexes := w.matchSecrets(lines) 87 | 88 | var linesToPrint [][]byte 89 | linesToPrint, w.store = w.matchLines(lines, partialMatchIndexes) 90 | if linesToPrint == nil { 91 | continue 92 | } 93 | 94 | redactedLines := w.redact(linesToPrint, matchMap) 95 | redactedBytes := bytes.Join(redactedLines, nil) 96 | if c, err := w.writer.Write(redactedBytes); err != nil { 97 | return c, err 98 | } 99 | } 100 | 101 | // it is necessary to return the count of incoming bytes 102 | // to let the exec.Command work properly 103 | return len(p), nil 104 | } 105 | 106 | // Close implements io.Writer interface 107 | func (w *Writer) Close() error { 108 | _, err := w.flush() 109 | return err 110 | } 111 | 112 | // flush writes the remaining bytes. 113 | func (w *Writer) flush() (int, error) { 114 | defer func() { 115 | w.mux.Unlock() 116 | }() 117 | w.mux.Lock() 118 | 119 | if len(w.chunk) > 0 { 120 | // lines are containing newline, chunk may not 121 | chunk := w.chunk 122 | w.chunk = nil 123 | w.store = append(w.store, chunk) 124 | } 125 | 126 | // we only need to care about the full matches in the remaining lines 127 | // (no more lines were come, why care about the partial matches?) 128 | matchMap, _ := w.matchSecrets(w.store) 129 | redactedLines := w.redact(w.store, matchMap) 130 | w.store = nil 131 | 132 | return w.writer.Write(bytes.Join(redactedLines, nil)) 133 | } 134 | 135 | // matchSecrets collects which secrets matches from which line indexes 136 | // and which secrets matches partially from which line indexes. 137 | // matchMap: matching line chunk's first line indexes by secret index 138 | // partialMatchIndexes: line indexes from which secrets matching but not fully contained in lines 139 | func (w *Writer) matchSecrets(lines [][]byte) (matchMap map[int][]int, partialMatchIndexes map[int]bool) { 140 | matchMap = make(map[int][]int) 141 | partialMatchIndexes = make(map[int]bool) 142 | 143 | for secretIdx, secret := range w.secrets { 144 | secretLine := secret[0] // every match should begin from the secret first line 145 | var lineIndexes []int // the indexes of lines which contains the secret's first line 146 | 147 | for i, line := range lines { 148 | if bytes.Contains(line, secretLine) { 149 | lineIndexes = append(lineIndexes, i) 150 | } 151 | } 152 | 153 | if len(lineIndexes) == 0 { 154 | // this secret can not be found in the lines 155 | continue 156 | } 157 | 158 | for _, lineIdx := range lineIndexes { 159 | if len(secret) == 1 { 160 | // the single line secret found in the lines 161 | indexes := matchMap[secretIdx] 162 | matchMap[secretIdx] = append(indexes, lineIdx) 163 | continue 164 | } 165 | 166 | // lineIdx. line matches to a multi line secret's first line 167 | // if lines has more line, every subsequent line must match to the secret's subsequent lines 168 | partialMatch := true 169 | match := false 170 | 171 | for i := lineIdx + 1; i < len(lines); i++ { 172 | secretLineIdx := i - lineIdx 173 | 174 | secretLine = secret[secretLineIdx] 175 | line := lines[i] 176 | 177 | if !bytes.Contains(line, secretLine) { 178 | partialMatch = false 179 | break 180 | } 181 | 182 | if secretLineIdx == len(secret)-1 { 183 | // multi line secret found in the lines 184 | match = true 185 | break 186 | } 187 | } 188 | 189 | if match { 190 | // multi line secret found in the lines 191 | indexes := matchMap[secretIdx] 192 | matchMap[secretIdx] = append(indexes, lineIdx) 193 | continue 194 | } 195 | 196 | if partialMatch { 197 | // this secret partially can be found in the lines 198 | partialMatchIndexes[lineIdx] = true 199 | } 200 | } 201 | } 202 | 203 | return 204 | } 205 | 206 | // linesToKeepRange returns the first line index needs to be observed 207 | // since they contain partially matching secrets. 208 | func (w *Writer) linesToKeepRange(partialMatchIndexes map[int]bool) int { 209 | first := -1 210 | 211 | for lineIdx := range partialMatchIndexes { 212 | if first == -1 { 213 | first = lineIdx 214 | continue 215 | } 216 | 217 | if first > lineIdx { 218 | first = lineIdx 219 | } 220 | } 221 | 222 | return first 223 | } 224 | 225 | // matchLines return which lines can be printed and which should be kept for further observing. 226 | func (w *Writer) matchLines(lines [][]byte, partialMatchIndexes map[int]bool) ([][]byte, [][]byte) { 227 | first := w.linesToKeepRange(partialMatchIndexes) 228 | switch first { 229 | case -1: 230 | // no lines need to be kept 231 | return lines, nil 232 | case 0: 233 | // partial match is always longer then the lines 234 | return nil, lines 235 | default: 236 | return lines[:first], lines[first:] 237 | } 238 | } 239 | 240 | // secretLinesToRedact returns which secret lines should be redacted 241 | func (w *Writer) secretLinesToRedact(lineIdxToRedact int, matchMap map[int][]int) [][]byte { 242 | // which line is which secrets first matching line 243 | secretIdxsByLine := make(map[int][]int) 244 | for secretIdx, lineIndexes := range matchMap { 245 | for _, lineIdx := range lineIndexes { 246 | secretIdxsByLine[lineIdx] = append(secretIdxsByLine[lineIdx], secretIdx) 247 | } 248 | } 249 | 250 | var secretChunks [][]byte 251 | for firstMatchingLineIdx, secretIndexes := range secretIdxsByLine { 252 | if lineIdxToRedact < firstMatchingLineIdx { 253 | continue 254 | } 255 | 256 | for _, secretIdx := range secretIndexes { 257 | secret := w.secrets[secretIdx] 258 | 259 | if lineIdxToRedact > firstMatchingLineIdx+len(secret)-1 { 260 | continue 261 | } 262 | 263 | secretLineIdx := lineIdxToRedact - firstMatchingLineIdx 264 | secretChunks = append(secretChunks, secret[secretLineIdx]) 265 | } 266 | } 267 | 268 | sort.Slice(secretChunks, func(i, j int) bool { return len(secretChunks[i]) < len(secretChunks[j]) }) 269 | return secretChunks 270 | } 271 | 272 | // redact hides the given ranges in the given line. 273 | func redact(line []byte, ranges []matchRange) []byte { 274 | var offset int // the offset of ranges generated by replacing x bytes by the RedactStr 275 | for _, r := range ranges { 276 | length := r.last - r.first 277 | first := r.first + offset 278 | last := first + length 279 | 280 | toRedact := line[first:last] 281 | redactStr := RedactStr 282 | if bytes.HasSuffix(toRedact, []byte("\n")) { 283 | // if string to redact ends with newline redact message should also 284 | redactStr += "\n" 285 | } 286 | 287 | newLine := append([]byte{}, line[:first]...) 288 | newLine = append(newLine, redactStr...) 289 | newLine = append(newLine, line[last:]...) 290 | 291 | offset += len(redactStr) - length 292 | 293 | line = newLine 294 | } 295 | return line 296 | } 297 | 298 | // redact hides the given secrets in the given lines. 299 | func (w *Writer) redact(lines [][]byte, matchMap map[int][]int) [][]byte { 300 | secretIdxsByLine := map[int][]int{} 301 | for secretIdx, lineIndexes := range matchMap { 302 | for _, lineIdx := range lineIndexes { 303 | secretIdxsByLine[lineIdx] = append(secretIdxsByLine[lineIdx], secretIdx) 304 | } 305 | } 306 | 307 | for i, line := range lines { 308 | linesToRedact := w.secretLinesToRedact(i, matchMap) 309 | if linesToRedact == nil { 310 | continue 311 | } 312 | 313 | var ranges []matchRange 314 | for _, lineToRedact := range linesToRedact { 315 | ranges = append(ranges, allRanges(line, lineToRedact)...) 316 | } 317 | 318 | lines[i] = redact(line, mergeAllRanges(ranges)) 319 | } 320 | 321 | return lines 322 | } 323 | 324 | // secretsByteList returns the list of secret byte lines. 325 | func secretsByteList(secrets []string) [][][]byte { 326 | var s [][][]byte 327 | for _, secret := range secrets { 328 | lines, lastLine := splitAfterNewline([]byte(secret)) 329 | if lines == nil && lastLine == nil { 330 | continue 331 | } 332 | 333 | var secretLines [][]byte 334 | if lines != nil { 335 | secretLines = append(secretLines, lines...) 336 | } 337 | if lastLine != nil { 338 | secretLines = append(secretLines, lastLine) 339 | } 340 | s = append(s, secretLines) 341 | } 342 | return s 343 | } 344 | 345 | // splitAfterNewline splits p after "\n", the split is assigned to lines 346 | // if last line has no "\n" it is assigned to the chunk. 347 | // If p is nil both lines and chunk is set to nil. 348 | func splitAfterNewline(p []byte) ([][]byte, []byte) { 349 | chunk := p 350 | var lines [][]byte 351 | 352 | for len(chunk) > 0 { 353 | idx := bytes.Index(chunk, newLine) 354 | if idx == -1 { 355 | return lines, chunk 356 | } 357 | 358 | lines = append(lines, chunk[:idx+1]) 359 | 360 | if idx == len(chunk)-1 { 361 | chunk = nil 362 | break 363 | } 364 | 365 | chunk = chunk[idx+1:] 366 | } 367 | 368 | return lines, chunk 369 | } 370 | -------------------------------------------------------------------------------- /redactwriter/redactwriter_test.go: -------------------------------------------------------------------------------- 1 | //go:build !race 2 | 3 | package redactwriter 4 | 5 | import ( 6 | "bytes" 7 | "fmt" 8 | "runtime" 9 | "testing" 10 | 11 | "github.com/bitrise-io/go-utils/v2/mocks" 12 | 13 | "github.com/bitrise-io/go-utils/v2/log" 14 | "github.com/stretchr/testify/require" 15 | ) 16 | 17 | func TestSecretsByteList(t *testing.T) { 18 | { 19 | secrets := []string{"secret value"} 20 | byteList := secretsByteList(secrets) 21 | require.Equal(t, [][][]byte{ 22 | [][]byte{ 23 | []byte("secret value"), 24 | }, 25 | }, byteList) 26 | } 27 | 28 | { 29 | secrets := []string{"secret value1", "secret value2"} 30 | byteList := secretsByteList(secrets) 31 | require.Equal(t, [][][]byte{ 32 | [][]byte{ 33 | []byte("secret value1"), 34 | }, 35 | [][]byte{ 36 | []byte("secret value2"), 37 | }, 38 | }, byteList) 39 | } 40 | 41 | { 42 | secrets := []string{"multi\nline\nsecret"} 43 | byteList := secretsByteList(secrets) 44 | require.Equal(t, [][][]byte{ 45 | [][]byte{ 46 | []byte("multi\n"), 47 | []byte("line\n"), 48 | []byte("secret"), 49 | }, 50 | }, byteList) 51 | } 52 | 53 | { 54 | secrets := []string{"ending\nwith\nnewline\n"} 55 | byteList := secretsByteList(secrets) 56 | require.Equal(t, [][][]byte{ 57 | [][]byte{ 58 | []byte("ending\n"), 59 | []byte("with\n"), 60 | []byte("newline\n"), 61 | }, 62 | }, byteList) 63 | } 64 | 65 | { 66 | secrets := []string{"\nstarting\nwith\nnewline"} 67 | byteList := secretsByteList(secrets) 68 | require.Equal(t, [][][]byte{ 69 | [][]byte{ 70 | []byte("\n"), 71 | []byte("starting\n"), 72 | []byte("with\n"), 73 | []byte("newline"), 74 | }, 75 | }, byteList) 76 | } 77 | 78 | { 79 | secrets := []string{"newlines\nin\n\nthe\n\n\nmiddle"} 80 | byteList := secretsByteList(secrets) 81 | require.Equal(t, [][][]byte{ 82 | [][]byte{ 83 | []byte("newlines\n"), 84 | []byte("in\n"), 85 | []byte("\n"), 86 | []byte("the\n"), 87 | []byte("\n"), 88 | []byte("\n"), 89 | []byte("middle"), 90 | }, 91 | }, byteList) 92 | } 93 | } 94 | 95 | func TestWrite(t *testing.T) { 96 | t.Log("trivial test") 97 | { 98 | var buff bytes.Buffer 99 | var mockLogger log.Logger = new(mocks.Logger) 100 | out := New([]string{"abc", "a\nb\nc"}, &buff, mockLogger) 101 | log := []byte("test with\nnew line\nand single line secret:abc\nand multiline secret:a\nb\nc") 102 | wc, err := out.Write(log) 103 | require.NoError(t, err) 104 | require.Equal(t, len(log), wc) 105 | 106 | err = out.Close() 107 | require.NoError(t, err) 108 | require.Equal(t, "test with\nnew line\nand single line secret:[REDACTED]\nand multiline secret:[REDACTED]\n[REDACTED]\n[REDACTED]", buff.String()) 109 | } 110 | 111 | t.Log("chunk without newline") 112 | { 113 | var buff bytes.Buffer 114 | var mockLogger log.Logger = new(mocks.Logger) 115 | out := New([]string{"ab", "a\nb"}, &buff, mockLogger) 116 | log := []byte("test without newline, secret:ab") 117 | wc, err := out.Write(log) 118 | require.NoError(t, err) 119 | require.Equal(t, len(log), wc) 120 | 121 | err = out.Close() 122 | require.NoError(t, err) 123 | require.Equal(t, "test without newline, secret:[REDACTED]", buff.String()) 124 | } 125 | 126 | t.Log("multiple secret in the same line") 127 | { 128 | var buff bytes.Buffer 129 | var mockLogger log.Logger = new(mocks.Logger) 130 | out := New([]string{"x1", "x\n2"}, &buff, mockLogger) 131 | log := []byte("multiple secrets like: x1 and x\n2 and some extra text") 132 | wc, err := out.Write(log) 133 | require.NoError(t, err) 134 | require.Equal(t, len(log), wc) 135 | 136 | err = out.Close() 137 | require.NoError(t, err) 138 | require.Equal(t, "multiple secrets like: [REDACTED] and [REDACTED]\n[REDACTED] and some extra text", buff.String()) 139 | } 140 | 141 | maxRun := 150000 142 | t.Log("multiple secret in the same line with multiple goroutine ") 143 | { 144 | cherr := make(chan error, maxRun) 145 | chStr := make(chan string, maxRun) 146 | 147 | var buff bytes.Buffer 148 | var mockLogger log.Logger = new(mocks.Logger) 149 | out := New([]string{"x1", "x\n2"}, &buff, mockLogger) 150 | log := []byte("multiple secrets like: x1 and x\n2 and some extra text") 151 | for i := 0; i < maxRun; i++ { 152 | go func(buff bytes.Buffer, out *Writer, log []byte) { 153 | runtime.Gosched() 154 | buff.Reset() 155 | 156 | wc, err := out.Write(log) 157 | require.NoError(t, err) 158 | require.Equal(t, len(log), wc) 159 | 160 | err = out.Close() 161 | 162 | cherr <- err 163 | chStr <- buff.String() 164 | }(buff, out, log) 165 | } 166 | 167 | errCounter := 0 168 | for err := range cherr { 169 | require.NoError(t, err) 170 | 171 | errCounter++ 172 | if errCounter == maxRun { 173 | close(cherr) 174 | } 175 | } 176 | 177 | strCounter := 0 178 | for str := range chStr { 179 | fmt.Println(str) 180 | 181 | strCounter++ 182 | if strCounter == maxRun { 183 | close(chStr) 184 | } 185 | } 186 | } 187 | } 188 | 189 | func TestSecrets(t *testing.T) { 190 | secrets := []string{ 191 | "a\nb\nc", 192 | "b", 193 | "c\nb", 194 | "x\nc\nb\nd", 195 | "f", 196 | } 197 | 198 | var buff bytes.Buffer 199 | var mockLogger log.Logger = new(mocks.Logger) 200 | out := New(secrets, &buff, mockLogger) 201 | require.Equal(t, [][][]byte{ 202 | [][]byte{[]byte("a\n"), []byte("b\n"), []byte("c")}, 203 | [][]byte{[]byte("b")}, 204 | [][]byte{[]byte("c\n"), []byte("b")}, 205 | [][]byte{[]byte("x\n"), []byte("c\n"), []byte("b\n"), []byte("d")}, 206 | [][]byte{[]byte("f")}, 207 | [][]byte{[]byte(`a\nb\nc`)}, 208 | [][]byte{[]byte(`c\nb`)}, 209 | [][]byte{[]byte(`x\nc\nb\nd`)}, 210 | }, out.secrets) 211 | } 212 | 213 | func TestMatchSecrets(t *testing.T) { 214 | secrets := []string{ 215 | "a\nb\nc", 216 | "b", 217 | "c\nb", 218 | "x\nc\nb\nd", 219 | "f", 220 | } 221 | lines := [][]byte{ 222 | []byte("x\n"), 223 | []byte("a\n"), 224 | []byte("a\n"), 225 | []byte("b\n"), 226 | []byte("c\n"), 227 | []byte("x\n"), 228 | []byte("c\n"), 229 | []byte("b\n")} 230 | 231 | var buff bytes.Buffer 232 | var mockLogger log.Logger = new(mocks.Logger) 233 | out := New(secrets, &buff, mockLogger) 234 | 235 | matchMap, partialMatchMap := out.matchSecrets(lines) 236 | require.Equal(t, map[int][]int{ 237 | 0: []int{2}, 238 | 1: []int{3, 7}, 239 | 2: []int{6}, 240 | }, matchMap) 241 | require.Equal(t, map[int]bool{5: true}, partialMatchMap) 242 | } 243 | 244 | func TestLinesToKeepRange(t *testing.T) { 245 | t.Log() 246 | secrets := []string{ 247 | "a\nb\nc", 248 | "b", 249 | "c\nb", 250 | "x\nc\nb\nd", 251 | } 252 | // lines := [][]byte{ 253 | // []byte("x\n"), 254 | // []byte("a\n"), 255 | // []byte("a\n"), 256 | // []byte("b\n"), 257 | // []byte("c\n"), 258 | // []byte("x\n"), 5.line 259 | // []byte("c\n"), 260 | // []byte("b\n")} 261 | 262 | var buff bytes.Buffer 263 | var mockLogger log.Logger = new(mocks.Logger) 264 | out := New(secrets, &buff, mockLogger) 265 | 266 | partialMatchMap := map[int]bool{6: true, 2: true, 5: true, 7: true} 267 | first := out.linesToKeepRange(partialMatchMap) // minimal index in the partialMatchMap 268 | require.Equal(t, 2, first) 269 | } 270 | 271 | func TestMatchLine(t *testing.T) { 272 | secrets := []string{ 273 | "a\nb\nc", 274 | "b", 275 | "c\nb", 276 | "x\nc\nb\nd", 277 | "f", 278 | } 279 | lines := [][]byte{ 280 | []byte("x\n"), // 0. 281 | []byte("a\n"), 282 | []byte("a\n"), // 2. 283 | []byte("b\n"), 284 | []byte("c\n"), // 4. 285 | []byte("x\n"), 286 | []byte("c\n"), // 6. 287 | []byte("b\n")} 288 | 289 | var buff bytes.Buffer 290 | var mockLogger log.Logger = new(mocks.Logger) 291 | out := New(secrets, &buff, mockLogger) 292 | 293 | _, partialMatchMap := out.matchSecrets(lines) 294 | print, remaining := out.matchLines(lines, partialMatchMap) 295 | require.Equal(t, [][]byte{ 296 | []byte("x\n"), 297 | []byte("a\n"), 298 | []byte("a\n"), 299 | []byte("b\n"), 300 | []byte("c\n"), 301 | }, print) 302 | require.Equal(t, [][]byte{ 303 | []byte("x\n"), 304 | []byte("c\n"), 305 | []byte("b\n"), 306 | }, remaining) 307 | } 308 | 309 | func TestSecretLinesToRedact(t *testing.T) { 310 | secrets := []string{ 311 | "a\nb\nc", 312 | "b", 313 | } 314 | lines := [][]byte{ 315 | []byte("x\n"), 316 | []byte("a\n"), 317 | []byte("b\n"), 318 | []byte("c\n"), 319 | []byte("b\n"), 320 | } 321 | 322 | var buff bytes.Buffer 323 | var mockLogger log.Logger = new(mocks.Logger) 324 | out := New(secrets, &buff, mockLogger) 325 | 326 | matchMap, _ := out.matchSecrets(lines) 327 | require.Equal(t, map[int][]int{ 328 | 0: []int{1}, 329 | 1: []int{2, 4}, 330 | }, matchMap) 331 | 332 | secretLines := out.secretLinesToRedact(0, matchMap) 333 | require.Equal(t, ([][]byte)(nil), secretLines, fmt.Sprintf("%s\n", secretLines)) 334 | 335 | secretLines = out.secretLinesToRedact(1, matchMap) 336 | require.Equal(t, [][]byte{[]byte("a\n")}, secretLines, fmt.Sprintf("%s\n", secretLines)) 337 | 338 | secretLines = out.secretLinesToRedact(2, matchMap) 339 | require.Equal(t, [][]byte{[]byte("b"), []byte("b\n")}, secretLines, fmt.Sprintf("%s\n", secretLines)) 340 | 341 | secretLines = out.secretLinesToRedact(3, matchMap) 342 | require.Equal(t, [][]byte{[]byte("c")}, secretLines, fmt.Sprintf("%s\n", secretLines)) 343 | 344 | secretLines = out.secretLinesToRedact(4, matchMap) 345 | require.Equal(t, [][]byte{[]byte("b")}, secretLines, fmt.Sprintf("%s\n", secretLines)) 346 | } 347 | 348 | func TestRedactLine(t *testing.T) { 349 | t.Log("redacts the middle of the line") 350 | { 351 | line := []byte("asdfabcasdf") 352 | ranges := []matchRange{ // asdfabcasdf 353 | {first: 4, last: 7}, // ****abc**** 354 | } 355 | redacted := redact(line, ranges) 356 | require.Equal(t, []byte("asdf[REDACTED]asdf"), redacted, string(redacted)) 357 | } 358 | 359 | t.Log("redacts the begining of the line") 360 | { 361 | line := []byte("asdfabcasdf") 362 | ranges := []matchRange{ // asdfabcasdf 363 | {first: 0, last: 5}, // asdfa****** 364 | } 365 | redacted := redact(line, ranges) 366 | require.Equal(t, []byte("[REDACTED]bcasdf"), redacted, string(redacted)) 367 | } 368 | 369 | t.Log("redacts the end of the line") 370 | { 371 | line := []byte("asdfabcasdf") 372 | ranges := []matchRange{ // asdfabcasdf 373 | {first: 9, last: 11}, // *********df 374 | } 375 | redacted := redact(line, ranges) 376 | require.Equal(t, []byte("asdfabcas[REDACTED]"), redacted, string(redacted)) 377 | } 378 | 379 | t.Log("redacts multiple secrets") 380 | { 381 | line := []byte("asdfabcasdf") 382 | ranges := []matchRange{ // asdfabcasdf 383 | {first: 4, last: 7}, // ****abc**** 384 | {first: 8, last: 10}, // ********sd* 385 | } 386 | redacted := redact(line, ranges) 387 | require.Equal(t, []byte("asdf[REDACTED]a[REDACTED]f"), redacted, string(redacted)) 388 | } 389 | 390 | t.Log("redacts the whole line") 391 | { 392 | line := []byte("asdfabcasdf") 393 | ranges := []matchRange{ // asdfabcasdf 394 | {first: 0, last: 4}, // asdf******* 395 | {first: 7, last: 11}, // *******asdf 396 | {first: 3, last: 9}, // ***fabcas** 397 | } 398 | ranges = mergeAllRanges(ranges) 399 | redacted := redact(line, ranges) 400 | require.Equal(t, []byte("[REDACTED]"), redacted, string(redacted)) 401 | } 402 | } 403 | 404 | func TestRedact(t *testing.T) { 405 | secrets := []string{ 406 | "a\nb\nc", 407 | "b", 408 | } 409 | lines := [][]byte{ 410 | []byte("x\n"), 411 | []byte("a\n"), 412 | []byte("a\n"), 413 | []byte("b\n"), 414 | []byte("c\n"), 415 | } 416 | 417 | var buff bytes.Buffer 418 | var mockLogger log.Logger = new(mocks.Logger) 419 | out := New(secrets, &buff, mockLogger) 420 | 421 | matchMap := map[int][]int{0: []int{2}, 1: []int{3}} 422 | redacted := out.redact(lines, matchMap) 423 | require.Equal(t, [][]byte{ 424 | []byte("x\n"), 425 | []byte("a\n"), 426 | []byte(RedactStr + "\n"), 427 | []byte(RedactStr + "\n"), 428 | []byte(RedactStr + "\n"), 429 | }, redacted) 430 | 431 | { 432 | secrets := []string{ 433 | "106\n105", 434 | "99", 435 | } 436 | lines := [][]byte{ 437 | []byte("106\n"), 438 | []byte("105\n"), 439 | []byte("104\n"), 440 | []byte("103\n"), 441 | []byte("102\n"), 442 | []byte("101\n"), 443 | []byte("100\n"), 444 | []byte("99\n")} 445 | 446 | var buff bytes.Buffer 447 | var mockLogger log.Logger = new(mocks.Logger) 448 | out := New(secrets, &buff, mockLogger) 449 | 450 | matchMap := map[int][]int{ 451 | 0: []int{0}, 452 | 1: []int{7}, 453 | } 454 | redacted := out.redact(lines, matchMap) 455 | require.Equal(t, [][]byte{ 456 | []byte(RedactStr + "\n"), 457 | []byte(RedactStr + "\n"), 458 | []byte("104" + "\n"), 459 | []byte("103" + "\n"), 460 | []byte("102" + "\n"), 461 | []byte("101" + "\n"), 462 | []byte("100" + "\n"), 463 | []byte(RedactStr + "\n"), 464 | }, redacted, fmt.Sprintf("%s", redacted)) 465 | } 466 | } 467 | 468 | func TestSplitAfterNewline(t *testing.T) { 469 | t.Log("bytes") 470 | { 471 | require.Equal(t, []byte{}, []byte("")) 472 | } 473 | 474 | t.Log("empty test") 475 | { 476 | b := []byte{} 477 | lines, chunk := splitAfterNewline(b) 478 | require.Equal(t, [][]byte(nil), lines) 479 | require.Equal(t, []byte{}, chunk) 480 | } 481 | 482 | t.Log("empty test - empty string bytes") 483 | { 484 | b := []byte("") 485 | lines, chunk := splitAfterNewline(b) 486 | require.Equal(t, [][]byte(nil), lines) 487 | require.Equal(t, []byte{}, chunk) 488 | } 489 | 490 | t.Log("newline test") 491 | { 492 | b := []byte("\n") 493 | lines, chunk := splitAfterNewline(b) 494 | require.Equal(t, [][]byte{[]byte("\n")}, lines) 495 | require.Equal(t, []byte(nil), chunk) 496 | } 497 | 498 | t.Log("multi line test") 499 | { 500 | b := []byte(`line 1 501 | line 2 502 | line 3 503 | `) 504 | lines, chunk := splitAfterNewline(b) 505 | require.Equal(t, 3, len(lines)) 506 | require.Equal(t, []byte("line 1\n"), lines[0]) 507 | require.Equal(t, []byte("line 2\n"), lines[1]) 508 | require.Equal(t, []byte("line 3\n"), lines[2]) 509 | require.Equal(t, []byte(nil), chunk) 510 | } 511 | 512 | t.Log("multi line test - newlines") 513 | { 514 | b := []byte(` 515 | 516 | line 1 517 | 518 | line 2 519 | `) 520 | 521 | lines, chunk := splitAfterNewline(b) 522 | require.Equal(t, 5, len(lines)) 523 | require.Equal(t, []byte("\n"), lines[0]) 524 | require.Equal(t, []byte("\n"), lines[1]) 525 | require.Equal(t, []byte("line 1\n"), lines[2]) 526 | require.Equal(t, []byte("\n"), lines[3]) 527 | require.Equal(t, []byte("line 2\n"), lines[4]) 528 | require.Equal(t, []byte(nil), chunk) 529 | } 530 | 531 | t.Log("chunk test") 532 | { 533 | b := []byte("line 1") 534 | lines, chunk := splitAfterNewline(b) 535 | require.Equal(t, [][]byte(nil), lines) 536 | require.Equal(t, []byte("line 1"), chunk) 537 | } 538 | 539 | t.Log("chunk test") 540 | { 541 | b := []byte(`line 1 542 | line 2`) 543 | 544 | lines, chunk := splitAfterNewline(b) 545 | require.Equal(t, 1, len(lines)) 546 | require.Equal(t, []byte("line 1\n"), lines[0]) 547 | require.Equal(t, []byte("line 2"), chunk) 548 | } 549 | t.Log("chunk test") 550 | { 551 | b := []byte("test\n\ntest\n") 552 | 553 | lines, chunk := splitAfterNewline(b) 554 | require.Equal(t, 3, len(lines)) 555 | require.Equal(t, []byte("test\n"), lines[0]) 556 | require.Equal(t, []byte("\n"), lines[1]) 557 | require.Equal(t, []byte("test\n"), lines[2]) 558 | require.Equal(t, []byte(nil), chunk) 559 | } 560 | } 561 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "local>bitrise-io/renovate-config" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /retryhttp/retryhttp.go: -------------------------------------------------------------------------------- 1 | package retryhttp 2 | 3 | import ( 4 | "github.com/bitrise-io/go-utils/v2/log" 5 | "github.com/hashicorp/go-retryablehttp" 6 | ) 7 | 8 | // NewClient returns a retryable HTTP client with common defaults 9 | func NewClient(logger log.Logger) *retryablehttp.Client { 10 | client := retryablehttp.NewClient() 11 | client.Logger = &httpLogAdaptor{logger: logger} 12 | client.ErrorHandler = retryablehttp.PassthroughErrorHandler 13 | 14 | return client 15 | } 16 | 17 | // httpLogAdaptor adapts the retryablehttp.Logger interface to the go-utils logger. 18 | type httpLogAdaptor struct { 19 | logger log.Logger 20 | } 21 | 22 | // Printf implements the retryablehttp.Logger interface 23 | func (a *httpLogAdaptor) Printf(fmtStr string, vars ...interface{}) { 24 | a.logger.Debugf(fmtStr, vars...) 25 | } 26 | -------------------------------------------------------------------------------- /system/cpu.go: -------------------------------------------------------------------------------- 1 | package system 2 | 3 | import ( 4 | "strings" 5 | 6 | "golang.org/x/sys/unix" 7 | ) 8 | 9 | type cpu struct{} 10 | 11 | // CPU is a singleton that returns information about the CPU 12 | var CPU cpu 13 | 14 | // Architecture returns the CPU's architecture name as a string 15 | // 16 | // Some example return values: 17 | // - x86_64 18 | // - arm64 19 | func (cpu) Architecture() (string, error) { 20 | var utsname unix.Utsname 21 | err := unix.Uname(&utsname) 22 | if err != nil { 23 | return "", err 24 | } 25 | 26 | architecture := string(utsname.Machine[:]) 27 | architecture = strings.Trim(architecture, "\x00") 28 | return architecture, nil 29 | } 30 | 31 | // IsARM returns true if the CPU's architecture is ARM 32 | // 33 | // Specifically, it returns true if the architecture's name contains the string 'arm' 34 | func (cpu) IsARM() (bool, error) { 35 | cpuType, err := CPU.Architecture() 36 | 37 | if err != nil { 38 | return false, err 39 | } 40 | 41 | return strings.Contains(cpuType, "arm"), nil 42 | } 43 | -------------------------------------------------------------------------------- /system/cpu_test.go: -------------------------------------------------------------------------------- 1 | package system 2 | 3 | import ( 4 | "os/exec" 5 | "strings" 6 | "testing" 7 | 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | // These tests aren't perfectly isolated, because they depend on the environment 12 | // they're being run in, but they are better than having nothing. 13 | 14 | func TestCpu_Architecture(t *testing.T) { 15 | // Given 16 | unameCmd := exec.Command("uname", "-m") 17 | unameOut, err := unameCmd.Output() 18 | if err != nil { 19 | t.Errorf("uname failed: %v", err) 20 | } 21 | unameOutString := string(unameOut) 22 | unameOutString = strings.TrimSpace(unameOutString) 23 | 24 | // When 25 | architecture, err := CPU.Architecture() 26 | if err != nil { 27 | t.Errorf("CPU.Architecture() failed: %v", err) 28 | } 29 | 30 | // Then 31 | require.Equal(t, unameOutString, architecture) 32 | } 33 | 34 | func TestCpu_IsARM(t *testing.T) { 35 | // Given 36 | unameCmd := exec.Command("uname", "-m") 37 | unameOut, err := unameCmd.Output() 38 | if err != nil { 39 | t.Errorf("uname failed: %v", err) 40 | } 41 | unameOutString := string(unameOut) 42 | unameOutString = strings.TrimSpace(unameOutString) 43 | 44 | // When 45 | isARM, err := CPU.IsARM() 46 | if err != nil { 47 | t.Errorf("CPU.IsARM() failed: %v", err) 48 | } 49 | 50 | // Then 51 | require.Equal(t, strings.Contains(unameOutString, "arm"), isARM) 52 | } 53 | --------------------------------------------------------------------------------