Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dispatch init command #78

Merged
merged 14 commits into from
Jul 4, 2024
Merged

add dispatch init command #78

merged 14 commits into from
Jul 4, 2024

Conversation

chicoxyzzy
Copy link
Contributor

@chicoxyzzy chicoxyzzy commented Jun 21, 2024

This PR implements dispatch init command to initialize a new project.

Usage

dispatch init <template> [path]

Where <template> is a required option and [path] is optional.

Templates directory

On a first use the command will create an Application Data directory dispatch

  • Windows: The application data directory is typically located at %APPDATA% or %LOCALAPPDATA%.
  • macOS: The application data directory is typically located at $HOME/Library/Application Support.
  • Linux: The application data directory is typically located at $XDG_CONFIG_HOME or $HOME/.config.

The App Data directory is used to save templates oh a user's computer to make them available for use later without Internet.
The directory has follows structure:

dispatch/
  templates/
    foo/
    bar/
  templates.sha

Workflow

  1. dispatch init <template> will try to get the SHA of the latest commit from the repository and compare it to local SHA. If values differ, Dispatch CLI will download update for templates first.
  2. Dispatch CLI makes request to using GitHub REST API to download tarball of the latest commit and then extracts it into the Dispatch's App Data directory.
  3. Finally, it copes template's files from the App Data directory (provided as <template> option) to a current working directory or to the [path] if it was provided.

If the destination directory is not empty, user will be asked if they want to clear the directory

image

Try it

git checkout init_command
make build
./build/darwin/arm64/dispatch init cat-facts ~/dispatch-template-test

This will create cat-facts template in the dispatch-template-test of your home directory.
Note that build directory can be different for your computer's arch and OS

To clear templates cache

rm -rf ~/Library/Application\ Support/dispatch

if you use MacOS

Notes and open questions

  • Current PR uses https://github.com/dispatchrun/dispatch-examples as a templates repo for the demonstration purpose only. We should introduce a new repo for templates.
  • Should we introduce versioning for templates to make them compatible with different versions of SDKs? I think we should
  • We can improve the templates update by adding information about which templates have been added/removed/modified or/and show last N commit messages and link to the full diff on GitHub between two SHAs
  • Current implementation makes it pretty easy to add 3rd-party templates (as described in Scaffolding projects with dispatch init command #59), but
    • it is easy to just use git clone instead (although init's caching and templates updating features could be still useful)
    • we should signal user that it is a 3rd-party template and it's not maintained by dispatch.run
  • Should dispatch init go to the Dispatch Commands group or should it be in the Additional Commands in the dispatch -h output? it's in the Dispatch group

Closes #59

@chicoxyzzy
Copy link
Contributor Author

This also still needs tests and docs update, but I wanted to start discussion earlier.

Copy link
Contributor

@achille-roussel achille-roussel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a couple of tests would be amazing 👍

If end-to-end tests are difficult to craft, at least testing the core functions like those copying files to make sure they are correct would go a long way!

cli/init.go Outdated Show resolved Hide resolved
@chicoxyzzy
Copy link
Contributor Author

Ok, I think it's ready. I've also created a templates repo, please review it: https://github.com/dispatchrun/dispatch-templates

@vadimdemedes
Copy link

vadimdemedes commented Jul 2, 2024

"dispatch init will try to get the SHA of the latest commit from the repository and compare it to local SHA. If values differ, Dispatch CLI will propose user to update templates first."

Can we skip the question about updating templates and do it transparently to the user?

@chicoxyzzy
Copy link
Contributor Author

Yes, let's do it 👍

@achille-roussel
Copy link
Contributor

Looks like there's a test that's failing when I run it 🤔

$ make test
go build -o build/darwin/arm64/dispatch .
go test ./...
?   	github.com/dispatchrun/dispatch	[no test files]
--- FAIL: TestRunCommand (0.34s)
    --- FAIL: TestRunCommand/Run_with_env_variable_in_local_env_vars_has_priority_over_the_one_in_the_env_file (0.34s)
        run_test.go:125: Expected printenv in the output: 2024/07/03 16:25:28 INFO loading environment variables from file path=/var/folders/qc/zv1mt0j11sscgjygfx47nxlm0000gn/T/TestRunCommandRun_with_env_variable_in_local_env_vars_has_priority_over_the_one_in_the_env_file1550241151/001/test.env
            dispatch | 2024-07-03 16:25:28.027 starting session session_id=2tKgIdbajqVaIRM8ctdBcw

            Error: command 'printenv CHARACTER' exited unexpectedly
            Usage:
              dispatch run [flags]

            Flags:
              -e, --endpoint string   Host:port that the local application endpoint is listening on (default "127.0.0.1:8000")
              -h, --help              help for run
              -s, --session string    Optional session to resume
                  --verbose           Enable verbose logging

            Global Flags:
              -k, --api-key string    Dispatch API key (env: DISPATCH_API_KEY)
                  --env-file string   Path to .env file



FAIL
FAIL	github.com/dispatchrun/dispatch/cli	1.766s
FAIL
make: *** [test] Error 1

@achille-roussel
Copy link
Contributor

I'm curious whether we could leave the current directly as-is instead of removing all the files:

~/go/src/github.com/dispatchrun/dispatch/build/darwin/arm64 $ ./dispatch init python
Directory . is not empty. Do you want to overwrite it? [y/N]
y
Template python was created in /Users/achilleroussel/go/src/github.com/dispatchrun/dispatch/build/darwin/arm64

Even if we ask the user, removing their files seems like a dangerous choice to make... mistakes happen easily.

Another alternative could be to simply refuse to initialize a template in a non-empty directory, then we don't need to prompt them either.

cli/init.go Outdated Show resolved Hide resolved
cli/init.go Outdated Show resolved Hide resolved
@chicoxyzzy
Copy link
Contributor Author

Another alternative could be to simply refuse to initialize a template in a non-empty directory, then we don't need to prompt them either.

I agree. Fixed

@chicoxyzzy
Copy link
Contributor Author

Looks like there's a test that's failing when I run it 🤔

Weird. This is a test for the dispatch run command. I don't think it's blocking for this PR, but I will take a look what's wrong here. It looks like printenv is not installed on your device / OS.

@achille-roussel
Copy link
Contributor

It seems to be there:

$ which printenv
/usr/bin/printenv

I expect we would get a different error if the program wasn't found as well.

Copy link
Contributor

@achille-roussel achille-roussel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work on this 👏

@chicoxyzzy
Copy link
Contributor Author

I've created a separate issue for the failing test and other dispatch run improvements #80

@chicoxyzzy chicoxyzzy merged commit 1a83d3f into main Jul 4, 2024
3 checks passed
@chicoxyzzy chicoxyzzy deleted the init_command branch July 4, 2024 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scaffolding projects with dispatch init command
3 participants