Skip to content
Closed
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
18 changes: 10 additions & 8 deletions cmd/dagger/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"github.com/spf13/cobra"
)

var doCmd = &cobra.Command{
Use: "do",
Args: cobra.NoArgs,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf(`############################################################################################
func doCmd() *cobra.Command {
return &cobra.Command{
Use: "do",
Args: cobra.NoArgs,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf(`############################################################################################
dagger do has been refactored into a new dagger-cue CLI. #
Please find the latest documentation in the following link: https://docs.dagger.io/sdk/cue #
############################################################################################
`)
os.Exit(1)
},
os.Exit(1)
},
}
}

// version holds the complete version number. Filled in at linking time.
10 changes: 6 additions & 4 deletions cmd/dagger/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func withEngine(
ctx context.Context,
sessionToken string,
allowedLocalDirs []string,
cb engine.StartCallback,
) error {
var runnerHost string
Expand All @@ -21,10 +22,11 @@ func withEngine(
}

engineConf := &engine.Config{
Workdir: workdir,
ConfigPath: configPath,
SessionToken: sessionToken,
RunnerHost: runnerHost,
Workdir: workdir,
ConfigPath: configPath,
SessionToken: sessionToken,
RunnerHost: runnerHost,
AllowedLocalDirs: allowedLocalDirs,
}
if debugLogs {
engineConf.LogOutput = os.Stderr
Expand Down
46 changes: 31 additions & 15 deletions cmd/dagger/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,50 @@ package main
import (
"context"
"fmt"
"net"
"net/http"
"os"

"github.com/dagger/dagger/router"
"github.com/spf13/cobra"
)

var listenAddress string
const outputPrefix = "==> server listening on "

var listenCmd = &cobra.Command{
Use: "listen",
Aliases: []string{"l"},
Run: Listen,
Hidden: true,
Short: "Starts the engine server",
var (
listenAddress string
listenLocalDirs []string
)

func listenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "listen",
Aliases: []string{"l"},
Run: Listen,
Hidden: true,
Short: "Starts the engine server",
}

cmd.Flags().StringVarP(&listenAddress, "listen", "", ":8080", "Listen on network address ADDR")
cmd.Flags().StringSliceVar(&listenLocalDirs, "local-dirs", []string{}, "local directories to allow for syncing into containers")

return cmd
}

func Listen(cmd *cobra.Command, args []string) {
ctx := context.Background()
if err := withEngine(ctx, "", func(ctx context.Context, r *router.Router) error {
fmt.Fprintf(os.Stderr, "==> server listening on %s\n", listenAddress)
return http.ListenAndServe(listenAddress, r)
if err := withEngine(ctx, "", listenLocalDirs, func(ctx context.Context, r *router.Router) error {
l, err := net.Listen("tcp", listenAddress)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Error: %v\n", err)
os.Exit(1)
}
listenAddress = l.Addr().(*net.TCPAddr).String() // handles case where port is :0 and selected automatically

fmt.Fprintf(cmd.OutOrStderr(), "%s %s\n", outputPrefix, listenAddress)
return http.Serve(l, r)
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
fmt.Fprintf(cmd.ErrOrStderr(), "Error: %v\n", err)
os.Exit(1)
}
}

func init() {
listenCmd.Flags().StringVarP(&listenAddress, "listen", "", ":8080", "Listen on network address ADDR")
}
77 changes: 77 additions & 0 deletions cmd/dagger/listen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"bufio"
"context"
"io"
"net/http"
"strings"
"testing"
"time"

"github.com/Khan/genqlient/graphql"
"github.com/stretchr/testify/require"
)

func TestAllowedLocalDirs(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

allowedDir1 := t.TempDir()
allowedDir2 := t.TempDir()
allowedDir3 := t.TempDir()
notAllowedDir := t.TempDir()

cmd := rootCmd()
cmd.SetArgs([]string{
"listen",
"--listen", "localhost:0",
"--local-dirs", strings.Join([]string{allowedDir1, allowedDir2}, ","),
"--local-dirs", allowedDir3,
})

r, w := io.Pipe()
cmd.SetOut(w)

go func() {
if err := cmd.ExecuteContext(ctx); err != nil {
panic(err)
}
}()

outCh := make(chan string)
go func() {
defer close(outCh)
out, err := bufio.NewReader(r).ReadString('\n')
require.NoError(t, err)
outCh <- out
}()

var addr string
select {
case out := <-outCh:
var ok bool
_, addr, ok = strings.Cut(out, outputPrefix)
require.True(t, ok, "expected output to start with %q: %q", outputPrefix, out)
addr = strings.TrimSpace(addr)
case <-time.After(30 * time.Second):
t.Fatal("timeout waiting for output")
}

gql := graphql.NewClient("http://"+addr+"/query", &http.Client{})

for _, allowedDir := range []string{allowedDir1, allowedDir2, allowedDir3} {
req := graphql.Request{Query: `{host{directory(path:"` + allowedDir + `"){entries}}}`}
data := map[string]any{}
resp := graphql.Response{Data: &data}
err := gql.MakeRequest(ctx, &req, &resp)
require.NoError(t, err)
require.Empty(t, resp.Errors)
}

req := graphql.Request{Query: `{host{directory(path:"` + notAllowedDir + `"){entries}}}`}
data := map[string]any{}
resp := graphql.Response{Data: &data}
err := gql.MakeRequest(ctx, &req, &resp)
require.ErrorContains(t, err, "no access allowed")
}
40 changes: 21 additions & 19 deletions cmd/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@ var (
debugLogs bool
)

func init() {
rootCmd.PersistentFlags().StringVar(&workdir, "workdir", ".", "The host workdir loaded into dagger")
rootCmd.PersistentFlags().BoolVar(&debugLogs, "debug", false, "show buildkit debug logs")

rootCmd.AddCommand(
listenCmd,
doCmd,
versionCmd,
queryCmd,
runCmd,
func rootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dagger",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
workdir, configPath, err = engine.NormalizePaths(workdir, configPath)
return err
},
}

cmd.PersistentFlags().StringVar(&workdir, "workdir", ".", "The host workdir loaded into dagger")
cmd.PersistentFlags().BoolVar(&debugLogs, "debug", false, "show buildkit debug logs")

cmd.AddCommand(
listenCmd(),
doCmd(),
versionCmd(),
queryCmd(),
runCmd(),
)
}

var rootCmd = &cobra.Command{
Use: "dagger",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
workdir, configPath, err = engine.NormalizePaths(workdir, configPath)
return err
},
return cmd
}

func main() {
closer := tracing.Init()
if err := rootCmd.Execute(); err != nil {
if err := rootCmd().Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
closer.Close()
os.Exit(1)
Expand Down
Loading