Skip to content

Commit

Permalink
implement --offline mode (#326)
Browse files Browse the repository at this point in the history
* implement --offline mode

* do it at root

* fix lint

* fix bug

* comment

* fix typo

* fix other typos

* arg shorthand

* omar's feedback

* fix log

* offline mode for app cmd

* Revert "offline mode for app cmd"

This reverts commit 829023d.

* err in offline mode
  • Loading branch information
Ismail Arafa authored Dec 6, 2023
1 parent 34fd9de commit a8a5420
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ If you want to know more about why we built `tb`, check out our [blog post](http

The main requirement for using `tb` is having `docker` and `docker compose v2` installed.
See the [Docker installation instructions](https://docs.docker.com/get-docker/) and select your operating system for more details.
See the [Compose installation instructions](https://docs.docker.com/compose/install/) to insure you have Compose v2 installed.
See the [Compose installation instructions](https://docs.docker.com/compose/install/) to ensure you have Compose v2 installed.

If you are using macOS having the Xcode CLI tools is also required. These can be easily installed by running `xcode-select --install`.

Expand Down Expand Up @@ -84,7 +84,7 @@ tb up -p db
## Basic Usage
By default `tb` contains no services on it's own. Run `tb list` to confirm this. This will also generate a default `~/.tbrc.yml` which will need to be edited.
By default `tb` contains no services on its own. Run `tb list` to confirm this. This will also generate a default `~/.tbrc.yml` which will need to be edited.
To add services to `tb` you will need to add a registry. A registry is a GitHub repo that contains a list of services, playlists, and apps `tb` can run. You can read the documentation on registries [here](docs/registries.md).
Add a registry by editing `~/.tbrc.yml` and add a registry to the `registries:` section:
Expand All @@ -93,7 +93,7 @@ registries:
- name: org/registry-name
```

You run can run a service or playlist by running `tb up -s <service>` or `tb up -p <playlist>`. `tb` will install any dependencies it needs and then start your services in docker containers.
You can run a service or playlist by running `tb up -s <service>` or `tb up -p <playlist>`. `tb` will install any dependencies it needs and then start your services in docker containers.

`tb up` will start [lazydocker](https://github.com/jesseduffield/lazydocker). For more information about how to interact with it, check out [its README](https://github.com/jesseduffield/lazydocker/blob/master/README.md). You can quit lazydocker, and containers will continue to run. You can always run `lazydocker` again without having to restart your services.

Expand All @@ -117,11 +117,11 @@ Run `tb --help` to see the commands available. Run `tb <cmd> --help` to get help
`tb` can be configured through the `.tbrc.yml` file located in your home directory. `tb` will automatically create a basic `.tbrc.yml` for you if one doesn't exist.

### Toggling experimental mode
To to enable experimental mode set the `experimental` field to `true`. Experimental mode will give you access to any new features that are still in the process of being tested.
To enable experimental mode set the `experimental` field to `true`. Experimental mode will give you access to any new features that are still in the process of being tested.
Please be aware that you may encounter bugs with these features as they have not yet been deemed ready for general use.
Also, experimental mode is not covered by semver guarantees, so there could be breaking changes at any time.

If you would like to help use test new features, we would appreciate it if you could enable experimental mode and report any issues you encounter.
If you would like to help test new features, we would appreciate it if you could enable experimental mode and report any issues you encounter.

### Adding custom playlists
You can create custom playlists by adding a new object to the `playlists` property.
Expand Down
2 changes: 2 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type Container struct {
Engine *engine.Engine
Tracker progress.Tracker
Verbose bool
// OfflineMode skips any operations requiring internet connectivity
OfflineMode bool
// Ctx is the context that should be used within a command to carry deadlines and cancellation signals.
Ctx context.Context
// Logfile is the log file used by the logger to record verbose logs in case of an error.
Expand Down
5 changes: 5 additions & 0 deletions cli/commands/app/desktop/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package desktop

import (
"fmt"
"github.com/TouchBistro/goutils/fatal"
"github.com/TouchBistro/tb/cli"
"github.com/TouchBistro/tb/engine"
Expand Down Expand Up @@ -29,6 +30,10 @@ Run the build for a specific branch:
tb app desktop run TouchBistroServer --branch task/bug-631/fix-thing`,
RunE: func(cmd *cobra.Command, args []string) error {
if c.OfflineMode {
return fmt.Errorf("Cannot run desktop app in offline mode")
}

appName := args[0]
err := c.Engine.AppDesktopRun(c.Ctx, appName, engine.AppDesktopRunOptions{
Branch: opts.branch,
Expand Down
5 changes: 5 additions & 0 deletions cli/commands/app/ios/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ios

import (
"fmt"
"github.com/TouchBistro/goutils/fatal"
"github.com/TouchBistro/tb/cli"
"github.com/TouchBistro/tb/engine"
Expand Down Expand Up @@ -32,6 +33,10 @@ Run the build for specific branch in an iOS 12.3 iPad Air 2 simulator:
tb app ios run TouchBistro --ios-version 12.3 --device "iPad Air 2" --branch task/pay-631/fix-thing`,
RunE: func(cmd *cobra.Command, args []string) error {
if c.OfflineMode {
return fmt.Errorf("Cannot run iOS app in offline mode")
}

appName := args[0]
iosVersion, deviceName, err := resolveDeviceName(c, appName, opts.iosVersion, opts.deviceName)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type rootOptions struct {
noRegistryPull bool
verbose bool
offlineMode bool
}

func NewRootCommand(c *cli.Container, version string) *cobra.Command {
Expand Down Expand Up @@ -66,6 +67,7 @@ func NewRootCommand(c *cli.Container, version string) *cobra.Command {
}
}
c.Verbose = opts.verbose || cfg.DebugEnabled()
c.OfflineMode = opts.offlineMode

// Initialize logging
// Create a temp file to log to.
Expand Down Expand Up @@ -102,7 +104,7 @@ func NewRootCommand(c *cli.Container, version string) *cobra.Command {
checkVersion(cmd.Context(), version, c.Tracker)

// Determine how to proceed based on the type of command
initOpts := config.InitOptions{UpdateRegistries: !opts.noRegistryPull}
initOpts := config.InitOptions{UpdateRegistries: !opts.noRegistryPull && !opts.offlineMode}
switch cmd.Parent().Name() {
case "registry":
// No further action required for registry commands
Expand Down Expand Up @@ -136,6 +138,7 @@ func NewRootCommand(c *cli.Container, version string) *cobra.Command {

persistentFlags := rootCmd.PersistentFlags()
persistentFlags.BoolVar(&opts.noRegistryPull, "no-registry-pull", false, "Don't pull latest version of registries when tb is run")
persistentFlags.BoolVarP(&opts.offlineMode, "offline", "o", false, "Skip operations requiring internet connectivity")
persistentFlags.BoolVarP(&opts.verbose, "verbose", "v", false, "Enable verbose logging")
rootCmd.AddCommand(
appCommands.NewAppCommand(c),
Expand Down
1 change: 1 addition & 0 deletions cli/commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Run the postgres and localstack services directly:
SkipPreRun: opts.skipServicePreRun,
SkipDockerPull: opts.skipDockerPull,
SkipGitPull: opts.skipGitPull,
OfflineMode: c.OfflineMode,
})
if err != nil {
return &fatal.Error{
Expand Down
2 changes: 1 addition & 1 deletion engine/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (e *Engine) AppDesktopRun(ctx context.Context, appName string, opts AppDesk

// Download the app
appPath, err := progress.RunT(ctx, progress.RunOptions{
Message: fmt.Sprintf("Downloading iOS app %s", a.FullName()),
Message: fmt.Sprintf("Downloading Desktop app %s", a.FullName()),
}, func(ctx context.Context) (string, error) {
return e.downloadApp(ctx, a, app.TypeDesktop, op)
})
Expand Down
10 changes: 6 additions & 4 deletions engine/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type UpOptions struct {
// SkipGitPull skips pulling existing git repos to update them.
// Missing repos will still be cloned however.
SkipGitPull bool
// OfflineMode skips login strategies and pulling remote images
OfflineMode bool
}

// Up performs all necessary actions to prepare services and then starts them.
Expand All @@ -67,15 +69,15 @@ func (e *Engine) Up(ctx context.Context, opts UpOptions) error {
if err != nil {
return err
}
if err := e.prepareGitRepos(ctx, op, opts.SkipGitPull); err != nil {
if err := e.prepareGitRepos(ctx, op, opts.SkipGitPull || opts.OfflineMode); err != nil {
return err
}
if err := e.writeComposeFile(ctx, op); err != nil {
return err
}

tracker := progress.TrackerFromContext(ctx)
if len(e.loginStrategies) > 0 {
if len(e.loginStrategies) > 0 && !opts.OfflineMode {
loginStrategies := make([]login.Strategy, len(e.loginStrategies))
for i, name := range e.loginStrategies {
s, err := login.ParseStrategy(name)
Expand Down Expand Up @@ -120,7 +122,7 @@ func (e *Engine) Up(ctx context.Context, opts UpOptions) error {
tracker.Info("✔ Cleaned up previous docker state")

// Pull base images
if !opts.SkipDockerPull && len(e.baseImages) > 0 {
if !opts.SkipDockerPull && !opts.OfflineMode && len(e.baseImages) > 0 {
err := progress.RunParallel(ctx, progress.RunParallelOptions{
Message: "Pulling docker base images",
Count: len(e.baseImages),
Expand All @@ -140,7 +142,7 @@ func (e *Engine) Up(ctx context.Context, opts UpOptions) error {
}

// Pull service images
if !opts.SkipDockerPull {
if !opts.SkipDockerPull && !opts.OfflineMode {
var images []string
for _, s := range services {
if s.Mode == service.ModeRemote {
Expand Down

0 comments on commit a8a5420

Please sign in to comment.