Skip to content
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
6 changes: 5 additions & 1 deletion .github/actions/experimental-setup-dagger/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: 'Experimental test action for setting up latest dagger CLI'
description: 'Experimental test action for setting up latest dagger CLI'
inputs:
project:
description: 'subpath of repo containing the project to invoke'
default: ''
runs:
using: "composite"
steps:
Expand All @@ -11,7 +15,7 @@ runs:
shell: bash
env:
CGO_ENABLED: "0"
- run: echo "DAGGER_PROJECT=${{ github.repositoryUrl }}#${{ github.ref }}" >> $GITHUB_ENV
- run: echo "DAGGER_PROJECT=${{ github.repositoryUrl }}?ref=${{ github.ref }}&subpath=${{ inputs.project }}" >> $GITHUB_ENV
shell: bash
# This proves we are not reliant on the checkout
- run: rm -rf * .git
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/lint-daggerized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/experimental-setup-dagger
- run: dagger do --debug --config ./ci ci:sdk:go:lint --repo ${{ github.server_url }}/${{ github.repository }} --branch ${{ github.ref }}
with:
project: "ci"
- run: dagger do --debug ci:sdk:go:lint --repo ${{ github.server_url }}/${{ github.repository }} --branch ${{ github.ref }}
env:
_EXPERIMENTAL_DAGGER_JOURNAL: "/tmp/journal.log"
_EXPERIMENTAL_DAGGER_CLOUD_TOKEN: "p.eyJ1IjogIjFiZjEwMmRjLWYyZmQtNDVhNi1iNzM1LTgxNzI1NGFkZDU2ZiIsICJpZCI6ICIwYzhmMGY2Yy00YjY1LTRhODktYTI0YS0yN2NjNWNhNzNmNTcifQ.Em92UDP-KmcNd80Y-euTS2IrRkIE8qTisP3SXYkQi3c"
Expand Down
3 changes: 2 additions & 1 deletion ci/dagger.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "ci",
"sdk": "go"
"sdk": "go",
"root": ".."
}
79 changes: 19 additions & 60 deletions cmd/dagger/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
Expand All @@ -21,22 +20,19 @@ import (
)

var (
projectURI string
configPath string
outputPath string
)

const (
projectURIDefault = "."
commandSeparator = ":"
commandSeparator = ":"
)

func init() {
doCmd.PersistentFlags().StringVarP(&projectURI, "project", "p", projectURIDefault, "Location of the project root, either local path (e.g. \"/path/to/some/dir\") or a git repo (e.g. \"git://github.com/dagger/dagger#branchname\").")
doCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./dagger.json", "Path to dagger.json config file for the project, or a parent directory containing that file, relative to the project's root directory.")
doCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "", "If the command returns a file or directory, it will be written to this path. If --output is not specified, the file or directory will be written to the project's root directory when using a project loaded from a local dir.")
}

// project flags (--project) for do command are setup in project.go

var doCmd = &cobra.Command{
Use: "do",
DisableFlagParsing: true,
Expand Down Expand Up @@ -71,11 +67,24 @@ var doCmd = &cobra.Command{
return fmt.Errorf("failed to connect to dagger: %w", err)
}

proj, err := getProject(c)
proj, err := getProjectFlagConfig()
if err != nil {
return fmt.Errorf("failed to get project config: %w", err)
}
if proj.local != nil && outputPath == "" {
// default to outputting to the project root dir
rootDir, err := proj.local.rootDir()
if err != nil {
return fmt.Errorf("failed to get project root dir: %w", err)
}
outputPath = rootDir
}

loadedProj, err := proj.load(ctx, c)
if err != nil {
return fmt.Errorf("failed to get project schema: %w", err)
return fmt.Errorf("failed to load project: %w", err)
}
projCmds, err := proj.Commands(ctx)
projCmds, err := loadedProj.Commands(ctx)
if err != nil {
return fmt.Errorf("failed to get project commands: %w", err)
}
Expand Down Expand Up @@ -355,56 +364,6 @@ func addCmd(ctx context.Context, cmdStack []*cobra.Command, projCmd dagger.Proje
return returnCmds, nil
}

func getProject(c *dagger.Client) (*dagger.Project, error) {
projectURI, configPath := projectURI, configPath
if projectURI == "" || projectURI == projectURIDefault {
// it's unset or default value, use env if present
if v, ok := os.LookupEnv("DAGGER_PROJECT"); ok {
projectURI = v
}
}

url, err := url.Parse(projectURI)
if err != nil {
return nil, fmt.Errorf("failed to parse config path: %w", err)
}
switch url.Scheme {
case "": // local path
projectAbsPath, err := filepath.Abs(projectURI)
if err != nil {
return nil, fmt.Errorf("failed to get project absolute path: %w", err)
}
configAbsPath, err := filepath.Abs(configPath)
if err != nil {
return nil, fmt.Errorf("failed to get config absolute path: %w", err)
}
configRelPath, err := filepath.Rel(projectAbsPath, configAbsPath)
if err != nil {
return nil, fmt.Errorf("failed to get config relative path: %w", err)
}

if outputPath == "" {
outputPath = projectAbsPath
}

return c.Project().Load(c.Host().Directory(projectAbsPath), configRelPath), nil
case "git":
repo := url.Host + url.Path
// TODO:(sipsma) just change ref to be a query param too?
ref := url.Fragment
if ref == "" {
ref = "main"
}

gitProtocol := url.Query().Get("protocol")
if gitProtocol != "" {
repo = gitProtocol + "://" + repo
}
return c.Project().Load(c.Git(repo).Branch(ref).Tree(), configPath), nil
}
return nil, fmt.Errorf("unsupported scheme %s", url.Scheme)
}

func getCommandName(cmd *cobra.Command) string {
// name is like "dagger do a:b:c", we return just "a:b:c" here
nameSplit := strings.Split(cmd.Name(), " ")
Expand Down
1 change: 1 addition & 0 deletions cmd/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
queryCmd,
runCmd,
sessionCmd(),
projectCmd,
)
}

Expand Down
Loading