Skip to content

Commit 0dcc2c5

Browse files
authored
Merge pull request #22 from reeflective/filesystem
Refactor filesystem abstractions with spf13/afero filesystem. Format imports. Update dependencies.
2 parents fe94968 + 22447c7 commit 0dcc2c5

File tree

27 files changed

+182
-336
lines changed

27 files changed

+182
-336
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,14 @@ the library focuses on offering a toolset for "human teaming": that is, treating
5454
are either _teamclients_ or _teamservers_ of others, within a defined -generally restricted- team of
5555
users, which shall generally be strictly and securely authenticated.
5656

57-
-----
58-
## Components & Terms
59-
60-
The result consists in 2 Go packages (`client` and `server`) for programs needing to act as:
61-
- A **Team client**: a program, or one of its components, that needs to rely on a "remote" program peer
62-
to serve some functionality that is available to a team of users' tools. The program acting as a
63-
_teamclient_ may do so for things as simple as sending a message to the team, or as complicated as a
64-
compiler backend with which multiple client programs can send data to process and build.
65-
- A **Team server**: The remote, server-side counterpart of the software teamclient. Again, the
66-
teamserver can be doing anything, from simply notifying users' teamclient connections to all the team
67-
all the way to handling very complex and resource-hungry tasks that can only be ran on a server host.
68-
69-
Throughout this library and its documentation, various words are repeatedly employed:
70-
- _teamclient_ refers to either the client-specific toolset provided by this library
71-
(`team/client.Client` core type) or the software making use of this teamclient code.
72-
- _teamserver_ refers to either the server-specific toolset provided to make a program serve its
73-
functionality remotely, or to the tools embedding this code in order to do so.
74-
- _team tool/s_ might be used to refer to programs using either or all of the library components at
75-
large.
76-
7757
-----
7858
## CLI (Users)
7959

8060
The following extracts assume a program binary named `teamserver`, which is simply the root command
8161
of the server-side team code. In this case therefore, the binary program only purpose its to be a
82-
teamserver, with no application-specific logic, (and is therefore quite useless on its own):
62+
teamserver, with no application-specific logic, (and is therefore quite useless on its own).
63+
For example, if your application `cracker` makes use of a teamserver/client, all the following
64+
commands would look like `cracker teamserver daemon`, `cracker teamserver client users`, etc:
8365
```
8466
$ teamserver
8567
Manage the application server-side teamserver and users
@@ -156,6 +138,26 @@ teamclient users
156138
teamclient version
157139
```
158140

141+
-----
142+
## Components & Terms
143+
144+
The result consists in 2 Go packages (`client` and `server`) for programs needing to act as:
145+
- A **Team client**: a program, or one of its components, that needs to rely on a "remote" program peer
146+
to serve some functionality that is available to a team of users' tools. The program acting as a
147+
_teamclient_ may do so for things as simple as sending a message to the team, or as complicated as a
148+
compiler backend with which multiple client programs can send data to process and build.
149+
- A **Team server**: The remote, server-side counterpart of the software teamclient. Again, the
150+
teamserver can be doing anything, from simply notifying users' teamclient connections to all the team
151+
all the way to handling very complex and resource-hungry tasks that can only be ran on a server host.
152+
153+
Throughout this library and its documentation, various words are repeatedly employed:
154+
- _teamclient_ refers to either the client-specific toolset provided by this library
155+
(`team/client.Client` core type) or the software making use of this teamclient code.
156+
- _teamserver_ refers to either the server-specific toolset provided to make a program serve its
157+
functionality remotely, or to the tools embedding this code in order to do so.
158+
- _team tool/s_ might be used to refer to programs using either or all of the library components at
159+
large.
160+
159161
-----
160162
## API (Developers)
161163

@@ -341,13 +343,12 @@ This teamserver library aims to remain small, with a precise behavior and role.
341343
Overall, contributions and ideas should revolve around strenghening its core/transport code
342344
or around enhancing its interoperability with as much Go code/programs as possible.
343345

344-
- [ ] Use viper for configs.
345-
- [ ] Use afero filesystem.
346346
- [ ] Add support for encrypted sqlite by default.
347347
- [ ] Encrypt in-memory channels, or add option for it.
348348
- [ ] Simpler/different listener/dialer backend interfaces, if it appears needed.
349349
- [ ] Abstract away the client-side authentication, for pluggable auth/credential models.
350350
- [ ] Replace logrus entirely and restructure behind a single package used by both client/server.
351351
- [ ] Review/refine/strenghen the dialer/listener init/close/start process, if it appears needed.
352352
- [ ] `teamclient update` downloads latest version of the server binary + method to `team.Client` for it.
353+
- [ ] Implement tests for most sensitive paths (certificates management, database functioning, etc)
353354

client/client.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ package client
1919
*/
2020

2121
import (
22-
"os/user"
23-
"path/filepath"
2422
"runtime"
2523
"sync"
2624

25+
"github.com/sirupsen/logrus"
26+
2727
"github.com/reeflective/team"
2828
"github.com/reeflective/team/internal/assets"
2929
"github.com/reeflective/team/internal/version"
30-
"github.com/sirupsen/logrus"
3130
)
3231

3332
// Client is the core driver of an application teamclient.
@@ -122,15 +121,12 @@ func New(app string, client team.Client, options ...Options) (*Client, error) {
122121
client: client,
123122
connect: &sync.Once{},
124123
mutex: &sync.RWMutex{},
125-
fs: &assets.FS{},
126124
}
127125

128126
teamclient.apply(options...)
129127

130128
// Filesystem (in-memory or on disk)
131-
user, _ := user.Current()
132-
root := filepath.Join(user.HomeDir, "."+teamclient.name)
133-
teamclient.fs = assets.NewFileSystem(root, teamclient.opts.inMemory)
129+
teamclient.fs = assets.NewFileSystem(teamclient.opts.inMemory)
134130

135131
// Logging (if allowed)
136132
if err := teamclient.initLogging(); err != nil {

client/commands/commands.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525
"path/filepath"
2626
"strings"
2727

28-
"github.com/reeflective/team/client"
29-
"github.com/reeflective/team/internal/command"
30-
"github.com/rsteube/carapace"
31-
"github.com/rsteube/carapace/pkg/style"
28+
"github.com/carapace-sh/carapace"
29+
"github.com/carapace-sh/carapace/pkg/style"
3230
"github.com/spf13/cobra"
3331
"github.com/spf13/pflag"
32+
33+
"github.com/reeflective/team/client"
34+
"github.com/reeflective/team/internal/command"
3435
)
3536

3637
// Generate returns a command tree to embed in client applications connecting

client/commands/import.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"encoding/json"
2323
"fmt"
2424

25-
"github.com/reeflective/team/client"
26-
"github.com/reeflective/team/internal/command"
2725
"github.com/sirupsen/logrus"
2826
"github.com/spf13/cobra"
27+
28+
"github.com/reeflective/team/client"
29+
"github.com/reeflective/team/internal/command"
2930
)
3031

3132
func importCmd(cli *client.Client) func(cmd *cobra.Command, args []string) {

client/commands/users.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323
"time"
2424

2525
"github.com/jedib0t/go-pretty/v6/table"
26-
"github.com/reeflective/team/client"
27-
"github.com/reeflective/team/internal/command"
2826
"github.com/sirupsen/logrus"
2927
"github.com/spf13/cobra"
28+
29+
"github.com/reeflective/team/client"
30+
"github.com/reeflective/team/internal/command"
3031
)
3132

3233
func usersCmd(cli *client.Client) func(cmd *cobra.Command, args []string) error {

client/commands/version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"fmt"
2323
"time"
2424

25-
"github.com/reeflective/team/client"
26-
"github.com/reeflective/team/internal/command"
2725
"github.com/sirupsen/logrus"
2826
"github.com/spf13/cobra"
27+
28+
"github.com/reeflective/team/client"
29+
"github.com/reeflective/team/internal/command"
2930
)
3031

3132
func versionCmd(cli *client.Client) func(cmd *cobra.Command, args []string) error {

client/directories.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package client
1919
*/
2020

2121
import (
22+
"os"
2223
"os/user"
2324
"path/filepath"
2425

@@ -33,15 +34,11 @@ func (tc *Client) HomeDir() string {
3334
var dir string
3435

3536
// Note: very important not to combine the nested if here.
36-
if !tc.opts.inMemory {
37-
if tc.homeDir == "" {
38-
user, _ := user.Current()
39-
dir = filepath.Join(user.HomeDir, "."+tc.name)
40-
} else {
41-
dir = tc.homeDir
42-
}
37+
if tc.homeDir == "" {
38+
user, _ := user.Current()
39+
dir = filepath.Join(user.HomeDir, "."+tc.name)
4340
} else {
44-
dir = "." + tc.name
41+
dir = tc.homeDir
4542
}
4643

4744
err := tc.fs.MkdirAll(dir, assets.DirPerm)
@@ -82,11 +79,13 @@ func (tc *Client) LogsDir() string {
8279
// ConfigsDir returns the path to the remote teamserver configs directory
8380
// for this application (~/.app/teamclient/configs), creating the directory
8481
// if needed, or logging a fatal event if failing to create it.
82+
//
83+
// This uses the on-disk filesystem even if the teamclient is in memory mode.
8584
func (tc *Client) ConfigsDir() string {
8685
rootDir, _ := filepath.Abs(tc.TeamDir())
8786
dir := filepath.Join(rootDir, assets.DirConfigs)
8887

89-
err := tc.fs.MkdirAll(dir, assets.DirPerm)
88+
err := os.MkdirAll(dir, assets.DirPerm)
9089
if err != nil {
9190
tc.log().Errorf("cannot write to %s configs dir: %s", dir, err)
9291
}

client/log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323
"io"
2424
"path/filepath"
2525

26-
"github.com/reeflective/team/internal/log"
2726
"github.com/sirupsen/logrus"
27+
28+
"github.com/reeflective/team/internal/log"
2829
)
2930

3031
// NamedLogger returns a new logging "thread" with two fields (optional)

client/options.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package client
1919
*/
2020

2121
import (
22-
"fmt"
2322
"io"
2423
"os"
2524
"strings"
2625

27-
"github.com/reeflective/team/internal/assets"
2826
"github.com/sirupsen/logrus"
27+
28+
"github.com/reeflective/team/internal/assets"
2929
)
3030

3131
const noTeamdir = "no team subdirectory"
@@ -69,7 +69,7 @@ func (tc *Client) apply(options ...Options) {
6969
// set once when created.
7070
tc.initOpts.Do(func() {
7171
// Application home directory.
72-
homeDir := os.Getenv(fmt.Sprintf("%s_ROOT_DIR", strings.ToUpper(tc.name)))
72+
homeDir := os.Getenv(strings.ToUpper(tc.name) + "_ROOT_DIR")
7373
if homeDir != "" {
7474
tc.homeDir = homeDir
7575
} else {
@@ -114,7 +114,7 @@ func WithInMemory() Options {
114114
// to connect to, instead of using default on-disk user/application configurations.
115115
// This function will be very useful to library users who wish to implement specific
116116
// remote teamserver selection & connection strategies, depending on the domains and
117-
// and use cases of these tools.
117+
// use cases of these tools.
118118
func WithConfig(config *Config) Options {
119119
return func(opts *opts) {
120120
opts.config = config

go.mod

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
module github.com/reeflective/team
22

3-
go 1.21
3+
go 1.23.0
4+
5+
toolchain go1.24.2
46

57
require (
68
github.com/AlecAivazis/survey/v2 v2.3.7
9+
github.com/carapace-sh/carapace v1.8.1
710
github.com/gofrs/uuid v4.4.0+incompatible
811
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
912
github.com/jedib0t/go-pretty/v6 v6.4.6
1013
github.com/lib/pq v1.10.9
1114
github.com/ncruces/go-sqlite3 v0.8.4
1215
github.com/ncruces/go-sqlite3/gormlite v0.8.4
13-
github.com/psanford/memfs v0.0.0-20230130182539-4dbf7e3e865e
1416
github.com/rsteube/carapace v0.47.4
1517
github.com/sirupsen/logrus v1.9.3
16-
github.com/spf13/cobra v1.8.1
18+
github.com/spf13/afero v1.14.0
19+
github.com/spf13/cobra v1.9.1
1720
github.com/spf13/pflag v1.0.6
1821
google.golang.org/grpc v1.56.1
1922
google.golang.org/protobuf v1.31.0
20-
gorm.io/driver/mysql v1.5.1
21-
gorm.io/driver/postgres v1.5.2
22-
gorm.io/driver/sqlite v1.5.2
23-
gorm.io/gorm v1.25.2
23+
gorm.io/driver/mysql v1.5.7
24+
gorm.io/driver/postgres v1.5.9
25+
gorm.io/driver/sqlite v1.5.5
26+
gorm.io/gorm v1.25.10
2427
modernc.org/sqlite v1.23.1
2528
)
2629

2730
require (
31+
github.com/carapace-sh/carapace-shlex v1.0.1 // indirect
2832
github.com/dustin/go-humanize v1.0.1 // indirect
2933
github.com/go-sql-driver/mysql v1.7.0 // indirect
3034
github.com/golang/protobuf v1.5.3 // indirect
3135
github.com/google/uuid v1.3.0 // indirect
3236
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3337
github.com/jackc/pgpassfile v1.0.0 // indirect
3438
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
35-
github.com/jackc/pgx/v5 v5.3.1 // indirect
39+
github.com/jackc/pgx/v5 v5.5.5 // indirect
40+
github.com/jackc/puddle/v2 v2.2.1 // indirect
3641
github.com/jinzhu/inflection v1.0.0 // indirect
3742
github.com/jinzhu/now v1.1.5 // indirect
3843
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -48,13 +53,14 @@ require (
4853
github.com/rsteube/carapace-shlex v0.1.1 // indirect
4954
github.com/stretchr/testify v1.8.2 // indirect
5055
github.com/tetratelabs/wazero v1.4.0 // indirect
51-
golang.org/x/crypto v0.8.0 // indirect
52-
golang.org/x/mod v0.11.0 // indirect
53-
golang.org/x/net v0.9.0 // indirect
54-
golang.org/x/sys v0.11.0 // indirect
55-
golang.org/x/term v0.7.0 // indirect
56-
golang.org/x/text v0.12.0 // indirect
57-
golang.org/x/tools v0.6.0 // indirect
56+
golang.org/x/crypto v0.37.0 // indirect
57+
golang.org/x/mod v0.17.0 // indirect
58+
golang.org/x/net v0.39.0 // indirect
59+
golang.org/x/sync v0.13.0 // indirect
60+
golang.org/x/sys v0.32.0 // indirect
61+
golang.org/x/term v0.31.0 // indirect
62+
golang.org/x/text v0.24.0 // indirect
63+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
5864
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
5965
gopkg.in/yaml.v3 v3.0.1 // indirect
6066
lukechampine.com/uint128 v1.2.0 // indirect

0 commit comments

Comments
 (0)