Skip to content

Commit

Permalink
feat: Move cmd to pkg/app (#250)
Browse files Browse the repository at this point in the history
* feat: Move cmd to pkg/app

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Fix test failures

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Fix test failure

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Fix govet

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed Jun 9, 2022
1 parent 4db0a28 commit 08a75eb
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 84 deletions.
65 changes: 3 additions & 62 deletions cmd/envd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ import (
"os"

"github.com/cockroachdb/errors"
_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
_ "github.com/moby/buildkit/client/connhelper/kubepod"
_ "github.com/moby/buildkit/client/connhelper/podmancontainer"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"
"go.starlark.net/starlark"
"go.starlark.net/syntax"

"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/app"
"github.com/tensorchord/envd/pkg/version"
)

Expand All @@ -38,61 +32,8 @@ func run(args []string) (bool, error) {
fmt.Println(c.App.Name, version.Package, c.App.Version, version.Revision)
}

// TODO(gaocegege): Enclose the app, maybe create the struct envdApp.
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "envd"
app.Usage = "Build tools for data scientists"
app.Version = version.GetVersion().String()
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in logs",
},
&cli.StringFlag{
Name: flag.FlagBuildkitdImage,
Usage: "docker image to use for buildkitd",
Value: "docker.io/moby/buildkit:v0.10.3",
},
&cli.StringFlag{
Name: flag.FlagBuildkitdContainer,
Usage: "buildkitd container to use for buildkitd",
Value: "envd_buildkitd",
},
}

app.Commands = []*cli.Command{
CommandBootstrap,
CommandBuild,
CommandDestroy,
CommandGet,
CommandPause,
CommandResume,
CommandUp,
CommandVersion,
}

// Deal with debug flag.
var debugEnabled bool

app.Before = func(context *cli.Context) error {
debugEnabled = context.Bool("debug")

logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
if debugEnabled {
logrus.SetLevel(logrus.DebugLevel)
}

if err := home.Initialize(); err != nil {
return errors.Wrap(err, "failed to initialize home manager")
}

// TODO(gaocegege): Add a config struct to keep them.
viper.Set(flag.FlagBuildkitdContainer, context.String(flag.FlagBuildkitdContainer))
viper.Set(flag.FlagBuildkitdImage, context.String(flag.FlagBuildkitdImage))
return nil
}
return debugEnabled, app.Run(args)
app := app.New()
return app.Debug, app.Run(args)
}

func handleErr(debug bool, err error) {
Expand Down
95 changes: 95 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"github.com/cockroachdb/errors"
_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
_ "github.com/moby/buildkit/client/connhelper/kubepod"
_ "github.com/moby/buildkit/client/connhelper/podmancontainer"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/version"
)

type EnvdApp struct {
cli.App
Debug bool
}

func New() EnvdApp {
internalApp := cli.NewApp()
internalApp.EnableBashCompletion = true
internalApp.Name = "envd"
internalApp.Usage = "Build tools for data scientists"
internalApp.Version = version.GetVersion().String()
internalApp.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in logs",
},
&cli.StringFlag{
Name: flag.FlagBuildkitdImage,
Usage: "docker image to use for buildkitd",
Value: "docker.io/moby/buildkit:v0.10.3",
},
&cli.StringFlag{
Name: flag.FlagBuildkitdContainer,
Usage: "buildkitd container to use for buildkitd",
Value: "envd_buildkitd",
},
}

internalApp.Commands = []*cli.Command{
CommandBootstrap,
CommandBuild,
CommandDestroy,
CommandGet,
CommandPause,
CommandResume,
CommandUp,
CommandVersion,
}

// Deal with debug flag.
var debugEnabled bool

internalApp.Before = func(context *cli.Context) error {
debugEnabled = context.Bool("debug")

logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
if debugEnabled {
logrus.SetLevel(logrus.DebugLevel)
}

if err := home.Initialize(); err != nil {
return errors.Wrap(err, "failed to initialize home manager")
}

// TODO(gaocegege): Add a config struct to keep them.
viper.Set(flag.FlagBuildkitdContainer, context.String(flag.FlagBuildkitdContainer))
viper.Set(flag.FlagBuildkitdImage, context.String(flag.FlagBuildkitdImage))
return nil
}

return EnvdApp{
App: *internalApp,
Debug: debugEnabled,
}
}
2 changes: 1 addition & 1 deletion cmd/envd/bootstrap.go → pkg/app/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package app

import (
"github.com/cockroachdb/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/build.go → pkg/app/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"path/filepath"
Expand Down
8 changes: 6 additions & 2 deletions cmd/envd/build_test.go → pkg/app/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"context"
Expand All @@ -31,14 +31,18 @@ var _ = Describe("build command", func() {
}
BeforeEach(func() {
Expect(home.Initialize()).NotTo(HaveOccurred())
app := New()
err := app.Run([]string{"envd.test", "--debug", "bootstrap"})
Expect(err).NotTo(HaveOccurred())
cli, err := docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), buildContext)
Expect(err).NotTo(HaveOccurred())
})
When("given the right arguments", func() {
It("should build successfully", func() {
_, err := run(args)
app := New()
err := app.Run(args)
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/destroy.go → pkg/app/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/get.go → pkg/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
cli "github.com/urfave/cli/v2"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/get_env.go → pkg/app/get_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/get_env_dep.go → pkg/app/get_env_dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/get_image.go → pkg/app/get_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/get_image_dep.go → pkg/app/get_image_dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/pause.go → pkg/app/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"github.com/cockroachdb/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/resume.go → pkg/app/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"github.com/cockroachdb/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/envd/suite_test.go → pkg/app/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"testing"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/envd/up.go → pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"fmt"
Expand Down
10 changes: 7 additions & 3 deletions cmd/envd/up_test.go → pkg/app/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"context"
Expand All @@ -31,19 +31,23 @@ var _ = Describe("up command", func() {
}
BeforeEach(func() {
Expect(home.Initialize()).NotTo(HaveOccurred())
app := New()
err := app.Run([]string{"envd.test", "--debug", "bootstrap"})
Expect(err).NotTo(HaveOccurred())
cli, err := docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), buildContext)
Expect(err).NotTo(HaveOccurred())
})
When("given the right arguments", func() {
It("should up and destroy successfully", func() {
_, err := run(args)
app := New()
err := app.Run(args)
Expect(err).NotTo(HaveOccurred())
destroyArgs := []string{
"envd.test", "--debug", "destroy", "--path", buildContext,
}
_, err = run(destroyArgs)
err = app.Run(destroyArgs)
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down
3 changes: 2 additions & 1 deletion cmd/envd/version.go → pkg/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"fmt"

"github.com/tensorchord/envd/pkg/version"
"github.com/urfave/cli/v2"
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/buildkitd/buildkitd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *generalClient) maybeStart(ctx context.Context,
return "", err
}
}
c.logger.Debug("container is running, check if it's ready...")
c.logger.Debugf("container is running, check if it's ready at %s...", c.BuildkitdAddr())

if err := c.waitUntilConnected(ctx, connectingTimeout); err != nil {
return "", errors.Wrap(err, "failed to connect to buildkitd")
Expand All @@ -133,7 +133,7 @@ func (c generalClient) waitUntilConnected(
case <-time.After(interval):
connected, err := c.connected(ctxTimeout)
if err != nil {
logrus.Debug("failed to connect to buildkitd")
logrus.Debugf("failed to connect to buildkitd: %s", err.Error())
continue
}
if !connected {
Expand Down
Loading

0 comments on commit 08a75eb

Please sign in to comment.