Skip to content

Commit

Permalink
choore: organise sub packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ravisuhag committed Nov 26, 2024
1 parent abb0e1b commit 83f5052
Show file tree
Hide file tree
Showing 66 changed files with 77 additions and 743 deletions.
78 changes: 36 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# salt

[![GoDoc reference](https://img.shields.io/badge/godoc-reference-5272B4.svg)](https://godoc.org/github.com/raystack/salt)
![test workflow](https://github.com/raystack/salt/actions/workflows/verify.yaml/badge.svg)
![test workflow](https://github.com/raystack/salt/actions/workflows/test.yaml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/raystack/salt)](https://goreportcard.com/report/github.com/raystack/salt)

Shared libraries used in the Raystack ecosystem. Use at your own risk. Breaking changes should be anticipated.
Salt is a Golang utility library offering a variety of packages to simplify and enhance application development. It provides modular and reusable components for common tasks, including configuration management, CLI utilities, authentication, logging, and more.

## Installation

Expand All @@ -16,56 +16,50 @@ go get github.com/raystack/salt

## Pacakages

### Audit
### Configuration and Environment
- **`config`**
Utilities for managing application configurations using environment variables, files, or defaults.

Package for adding audit events in your applications.
### CLI Utilities
- **`cli/cmdx`**
Command execution and management tools.

### Cmdx
- **`cli/printer`**
Utilities for formatting and printing output to the terminal.

Cobra based cli helper which allows adding command groups, provides custom help and usage functions.
- **`cli/prompt`**
Interactive CLI prompts for user input.

```
var cmd = &cli.Command{
Use: "exec <command> <subcommand> [flags]",
SilenceUsage: true,
SilenceErrors: true,
Annotations: map[string]string{
"group": "core",
"help:learn": "Learn about the project",
},
}
cmdx.SetHelp(cmd)
cmd.AddCommand(cmdx.SetCompletionCmd("exec"))
cmd.AddCommand(cmdx.SetHelpTopicCmd("environment", envHelp))
cmd.AddCommand(cmdx.SetHelpTopicCmd("auth", authHelp))
cmd.AddCommand(cmdx.SetRefCmd(cmd))
```

### Config

Viper abstractions which provides functions for loading config files for the application.

### DB

Postgres based database abstractions for creating a client and running migrations.

### Log
- **`cli/terminal`**
Terminal utilities for colors, cursor management, and formatting.

Logger for easy application loggging.
- **`cli/version`**
Utilities for displaying and managing CLI tool versions.

### Printer
### Authentication and Security
- **`auth/oidc`**
Helpers for integrating OpenID Connect authentication flows.

Command line printer for CLI based applications.
- **`auth/audit`**
Auditing tools for tracking security events and compliance.

### Server
### Server and Infrastructure
- **`server`**
Utilities for setting up and managing HTTP or RPC servers.

GRPC based server abstraction.
- **`db`**
Helpers for database connections, migrations, and query execution.

### Term
- **`telemetry`**
Observability tools for capturing application metrics and traces.

Helper functions for working with terminal.
### Development and Testing
- **`dockertestx`**
Tools for creating and managing Docker-based testing environments.

### Version
### Utilities
- **`log`**
Simplified logging utilities for structured and unstructured log messages.

Helper functions for fetching github latest and outdated releases.
- **`utils`**
General-purpose utility functions for common programming tasks.
File renamed without changes.
48 changes: 24 additions & 24 deletions audit/audit_test.go → auth/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package audit_test
import (
"context"
"errors"
audit2 "github.com/raystack/salt/auth/audit"
"github.com/raystack/salt/auth/audit/mocks"
"testing"
"time"

"github.com/raystack/salt/audit"
"github.com/raystack/salt/audit/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
Expand All @@ -18,24 +18,24 @@ type AuditTestSuite struct {
now time.Time

mockRepository *mocks.Repository
service *audit.Service
service *audit2.Service
}

func (s *AuditTestSuite) setupTest() {
s.mockRepository = new(mocks.Repository)
s.service = audit.New(
audit.WithMetadataExtractor(func(context.Context) map[string]interface{} {
s.service = audit2.New(
audit2.WithMetadataExtractor(func(context.Context) map[string]interface{} {
return map[string]interface{}{
"trace_id": "test-trace-id",
"app_name": "guardian_test",
"app_version": 1,
}
}),
audit.WithRepository(s.mockRepository),
audit2.WithRepository(s.mockRepository),
)

s.now = time.Now()
audit.TimeNow = func() time.Time {
audit2.TimeNow = func() time.Time {
return s.now
}
}
Expand All @@ -48,7 +48,7 @@ func (s *AuditTestSuite) TestLog() {
s.Run("should insert to repository", func() {
s.setupTest()

s.mockRepository.On("Insert", mock.Anything, &audit.Log{
s.mockRepository.On("Insert", mock.Anything, &audit2.Log{
Timestamp: s.now,
Action: "action",
Actor: "[email protected]",
Expand All @@ -61,23 +61,23 @@ func (s *AuditTestSuite) TestLog() {
}).Return(nil)

ctx := context.Background()
ctx = audit.WithActor(ctx, "[email protected]")
ctx = audit2.WithActor(ctx, "[email protected]")
err := s.service.Log(ctx, "action", map[string]interface{}{"foo": "bar"})
s.NoError(err)
})

s.Run("actor extractor", func() {
s.Run("should use actor extractor if option given", func() {
expectedActor := "test-actor"
s.service = audit.New(
audit.WithActorExtractor(func(ctx context.Context) (string, error) {
s.service = audit2.New(
audit2.WithActorExtractor(func(ctx context.Context) (string, error) {
return expectedActor, nil
}),
audit.WithRepository(s.mockRepository),
audit2.WithRepository(s.mockRepository),
)

s.mockRepository.On("Insert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
log := args.Get(1).(*audit.Log)
log := args.Get(1).(*audit2.Log)
s.Equal(expectedActor, log.Actor)
}).Return(nil).Once()

Expand All @@ -87,8 +87,8 @@ func (s *AuditTestSuite) TestLog() {

s.Run("should return error if extractor returns error", func() {
expectedError := errors.New("test error")
s.service = audit.New(
audit.WithActorExtractor(func(ctx context.Context) (string, error) {
s.service = audit2.New(
audit2.WithActorExtractor(func(ctx context.Context) (string, error) {
return "", expectedError
}),
)
Expand All @@ -100,18 +100,18 @@ func (s *AuditTestSuite) TestLog() {

s.Run("metadata", func() {
s.Run("should pass empty trace id if extractor not found", func() {
s.service = audit.New(
audit.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} {
s.service = audit2.New(
audit2.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} {
return map[string]interface{}{
"app_name": "guardian_test",
"app_version": 1,
}
}),
audit.WithRepository(s.mockRepository),
audit2.WithRepository(s.mockRepository),
)

s.mockRepository.On("Insert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
l := args.Get(1).(*audit.Log)
l := args.Get(1).(*audit2.Log)
s.IsType(map[string]interface{}{}, l.Metadata)

md := l.Metadata.(map[string]interface{})
Expand All @@ -125,25 +125,25 @@ func (s *AuditTestSuite) TestLog() {
})

s.Run("should append new metadata to existing one", func() {
s.service = audit.New(
audit.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} {
s.service = audit2.New(
audit2.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} {
return map[string]interface{}{
"existing": "foobar",
}
}),
audit.WithRepository(s.mockRepository),
audit2.WithRepository(s.mockRepository),
)

expectedMetadata := map[string]interface{}{
"existing": "foobar",
"new": "foobar",
}
s.mockRepository.On("Insert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
log := args.Get(1).(*audit.Log)
log := args.Get(1).(*audit2.Log)
s.Equal(expectedMetadata, log.Metadata)
}).Return(nil).Once()

ctx, err := audit.WithMetadata(context.Background(), map[string]interface{}{
ctx, err := audit2.WithMetadata(context.Background(), map[string]interface{}{
"new": "foobar",
})
s.Require().NoError(err)
Expand Down
3 changes: 1 addition & 2 deletions audit/mocks/repository.go → auth/audit/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"database/sql"
"fmt"
"github.com/raystack/salt/auth/audit/repositories"
"time"

_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/raystack/salt/audit/repositories"
"github.com/raystack/salt/log"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/raystack/salt/auth/audit"
"time"

"github.com/jmoiron/sqlx/types"
"github.com/raystack/salt/audit"
)

type AuditModel struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package repositories_test

import (
"context"
"github.com/raystack/salt/auth/audit"
"github.com/raystack/salt/auth/audit/repositories"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/jmoiron/sqlx/types"
"github.com/raystack/salt/audit"
"github.com/raystack/salt/audit/repositories"
"github.com/raystack/salt/log"
"github.com/stretchr/testify/suite"
)
Expand Down
3 changes: 1 addition & 2 deletions oidc/_example/main.go → auth/oidc/_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"encoding/json"
"github.com/raystack/salt/auth/oidc"
"log"
"os"
"strings"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"

"github.com/raystack/salt/oidc"
)

func main() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmdx/ref.go → cli/cmdx/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cmdx
import (
"bytes"
"fmt"
"github.com/raystack/salt/cli/printer"
"io"
"strings"

"github.com/raystack/salt/printer"
"github.com/spf13/cobra"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion printer/spinner.go → cli/printer/spinner.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package printer

import (
"github.com/raystack/salt/cli/terminal"
"time"

"github.com/briandowns/spinner"
"github.com/raystack/salt/terminal"
)

type Indicator struct {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 0 additions & 8 deletions mux/README.md

This file was deleted.

Loading

0 comments on commit 83f5052

Please sign in to comment.