Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: consolidate paths into paths package #41

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/cmd/local/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/airbytehq/abctl/internal/cmd/local/localerr"
"github.com/airbytehq/abctl/internal/cmd/local/paths"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
Expand Down Expand Up @@ -101,9 +102,8 @@ func newWithOptions(ctx context.Context, newPing newPing, goos string) (*Docker,
// on mac, sometimes the docker host isn't set correctly, if it fails check the home directory
dockerCli, err = createAndPing(ctx, newPing, "unix:///var/run/docker.sock", dockerOpts)
if err != nil {
userHome, _ := os.UserHomeDir()
var err2 error
dockerCli, err2 = createAndPing(ctx, newPing, fmt.Sprintf("unix://%s/.docker/run/docker.sock", userHome), dockerOpts)
dockerCli, err2 = createAndPing(ctx, newPing, fmt.Sprintf("unix://%s/.docker/run/docker.sock", paths.UserHome), dockerOpts)
if err2 != nil {
return nil, fmt.Errorf("%w: could not create docker client: (%w, %w)", localerr.ErrDocker, err, err2)
}
Expand Down Expand Up @@ -209,8 +209,7 @@ func (d *Docker) MigrateComposeDB(ctx context.Context, volume string) error {
pterm.Debug.Println(fmt.Sprintf("Created initial migration container '%s'", conCopy.ID))

// docker cp [conCopy.ID]]:/$migratePGDATA/. ~/.airbyte/abctl/data/airbyte-volume-db/pgdata
userHome, _ := os.UserHomeDir()
dst := filepath.Join(userHome, ".airbyte", "abctl", "data", "airbyte-volume-db", "pgdata")
dst := filepath.Join(paths.Data, "airbyte-volume-db", "pgdata")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where this is going, i think we could go even a little further and add some convenience methods like ensureDir, ensureFile, etc. All of which would be relative to $HOME/.airbyte

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the plan, but I'll do that as part of a follow-up PR (after #40 has been merged)

// note the src must end with a `.`, due to how docker cp works with directories
if err := d.copyFromContainer(ctx, conCopy.ID, migratePGDATA+"/.", dst); err != nil {
return fmt.Errorf("could not copy airbyte db data from container %s: %w", conCopy.ID, err)
Expand Down
10 changes: 2 additions & 8 deletions internal/cmd/local/k8s/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package k8s

import (
"fmt"
"os"
"path/filepath"
"github.com/airbytehq/abctl/internal/cmd/local/paths"
"sigs.k8s.io/kind/pkg/cluster"
"time"
)
Expand Down Expand Up @@ -32,11 +31,6 @@ type kindCluster struct {

const k8sVersion = "v1.29.1"

var mountPath = func() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".airbyte", "abctl", "data")
}

func (k *kindCluster) Create(port int) error {
// see https://kind.sigs.k8s.io/docs/user/ingress/#create-cluster
rawCfg := fmt.Sprintf(`kind: Cluster
Expand All @@ -56,7 +50,7 @@ nodes:
- containerPort: 80
hostPort: %d
protocol: TCP`,
mountPath(),
paths.Data,
port)

opts := []cluster.CreateOption{
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/local/local/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/airbytehq/abctl/internal/cmd/local/docker"
"github.com/airbytehq/abctl/internal/cmd/local/paths"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -503,10 +504,9 @@ func (c *Command) Uninstall(_ context.Context, opts UninstallOpts) error {
// check if persisted data should be removed, if not this is a noop
if opts.Persisted {
c.spinner.UpdateText("Removing persisted data")
data := filepath.Join(c.userHome, ".airbyte", "abctl", "data")
if err := os.RemoveAll(data); err != nil {
pterm.Error.Println(fmt.Sprintf("Unable to remove persisted data '%s'", data))
return fmt.Errorf("could not remove persisted data '%s': %w", data, err)
if err := os.RemoveAll(paths.Data); err != nil {
pterm.Error.Println(fmt.Sprintf("Unable to remove persisted data '%s'", paths.Data))
return fmt.Errorf("could not remove persisted data '%s': %w", paths.Data, err)
}
pterm.Success.Println("Removed persisted data")
}
Expand Down
32 changes: 32 additions & 0 deletions internal/cmd/local/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package paths

import (
"os"
"path/filepath"
)

var (
// UserHome is the user's home directory
UserHome = func() string {
h, _ := os.UserHomeDir()
return h
}()
// Airbyte is the full path to the ~/.airbyte directory
Airbyte = airbyte()
// AbCtl is the full path to the ~/.airbyte/abctl directory
AbCtl = abctl()
// Data is the full path to the ~/.airbyte/abctl/data directory
Data = data()
)

func airbyte() string {
return filepath.Join(UserHome, ".airbyte")
}

func abctl() string {
return filepath.Join(airbyte(), "abctl")
}

func data() string {
return filepath.Join(abctl(), "data")
}
35 changes: 35 additions & 0 deletions internal/cmd/local/paths/paths_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package paths

import (
"github.com/google/go-cmp/cmp"
"os"
"path/filepath"
"testing"
)

func Test_Paths(t *testing.T) {
t.Run("UserHome", func(t *testing.T) {
exp, _ := os.UserHomeDir()
if d := cmp.Diff(exp, UserHome); d != "" {
t.Errorf("UserHome mismatch (-want +got):\n%s", d)
}
})
t.Run("Airbyte", func(t *testing.T) {
exp := filepath.Join(UserHome, ".airbyte")
if d := cmp.Diff(exp, Airbyte); d != "" {
t.Errorf("Airbyte mismatch (-want +got):\n%s", d)
}
})
t.Run("AbCtl", func(t *testing.T) {
exp := filepath.Join(UserHome, ".airbyte", "abctl")
if d := cmp.Diff(exp, AbCtl); d != "" {
t.Errorf("AbCtl mismatch (-want +got):\n%s", d)
}
})
t.Run("Data", func(t *testing.T) {
exp := filepath.Join(UserHome, ".airbyte", "abctl", "data")
if d := cmp.Diff(exp, Data); d != "" {
t.Errorf("Data mismatch (-want +got):\n%s", d)
}
})
}