commands

package
v0.0.0-...-0795abe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BinCommand = cmd.Command{
	Name:    "bin",
	Aliases: []string{"b"},
	Summary: "Motoko compiler stuff",
	Commands: []cmd.Command{
		BinDownloadCommand,
		BinShowCommand,
	},
}
View Source
var BinDownloadCommand = cmd.Command{
	Name:    "download",
	Aliases: []string{"d"},
	Summary: "downloads the Motoko compiler",
	Options: []cmd.Option{
		{
			Name:     "didc",
			HasValue: false,
		},
	},
	Method: func(_ []string, options map[string]string) error {
		pkg, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewBinError(err)
		}

		if pkg.CompilerVersion == nil {
			return NewBinError(NewCompilerVersionNotFoundError())
		}
		version := *pkg.CompilerVersion

		goos := runtime.GOOS
		switch goos {
		case "darwin":
			goos = "macos"
		case "linux":
			goos = "linux64"
		default:
			return NewBinError(NewUnsupportedRuntimeErrors(goos))
		}

		if err := tar.DownloadGz(
			fmt.Sprintf(
				"https://github.com/dfinity/motoko/releases/download/%s/motoko-%s-%s.tar.gz",
				version, goos, version,
			),
			fmt.Sprintf(".oko/bin/%s", version),
		); err != nil {
			return NewBinError(err)
		}

		if _, ok := options["didc"]; ok {
			url := "dfinity/candid"
			resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/releases", url))
			if err != nil {
				return NewBinError(err)
			}
			data, err := io.ReadAll(resp.Body)
			if err != nil {
				return NewBinError(err)
			}
			var releases []github.Release
			if err := json.Unmarshal(data, &releases); err != nil {
				return NewBinError(err)
			}
			if len(releases) == 0 {
				return NewBinError(github.NewReleasesNotFoundErrors(url))
			}

			resp, err = http.Get(fmt.Sprintf(
				"https://github.com/%s/releases/download/%s/didc-%s",
				url, releases[0].TagName, goos,
			))
			if err != nil {
				return NewBinError(err)
			}
			data, err = io.ReadAll(resp.Body)
			if err != nil {
				return NewBinError(err)
			}
			if err := os.WriteFile(fmt.Sprintf(".oko/bin/%s/didc", version), data, os.ModePerm); err != nil {
				return NewBinError(err)
			}
		}

		return nil
	},
}
View Source
var BinShowCommand = cmd.Command{
	Name:    "show",
	Aliases: []string{"s"},
	Summary: "prints out the path to the bin dir",
	Method: func(args []string, options map[string]string) error {
		pkg, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewBinError(err)
		}

		if pkg.CompilerVersion == nil {
			return NewBinError(NewCompilerVersionNotFoundError())
		}
		version := *pkg.CompilerVersion
		fmt.Printf(".oko/bin/%s", version)
		return nil
	},
}
View Source
var DownloadCommand = cmd.Command{
	Name:        "download",
	Aliases:     []string{"d"},
	Summary:     "download packages",
	Description: `Downloads all packages specified in the Oko package file.`,
	Method: func(_ []string, _ map[string]string) error {
		state, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewDownloadError(err)
		}
		if err := state.Download(); err != nil {
			return NewDownloadError(err)
		}
		return nil
	},
}
View Source
var InitCommand = cmd.Command{
	Name:        "init",
	Summary:     "initialize Oko",
	Description: `Initializes the Oko package file.`,
	Options: []cmd.Option{
		{
			Name:     "compiler",
			Summary:  "compiler version",
			HasValue: true,
		},
	},
	Method: func(_ []string, options map[string]string) error {
		if _, err := config.LoadPackageState("./oko.json"); err == nil {
			return NewInitError(err)
		}
		state := config.EmptyState()
		if v, ok := options["compiler"]; ok {
			state.CompilerVersion = &v
		}
		if err := state.Save("./oko.json"); err != nil {
			return NewInitError(err)
		}
		return nil
	},
}
View Source
var InstallCommand = cmd.Command{
	Name:        "install",
	Aliases:     []string{"i"},
	Summary:     "install packages",
	Description: `Allows you to install packages from GitHub or link local directories.`,
	Commands: []cmd.Command{
		InstallGitHubCommand,
		InstallLocalCommand,
	},
}
View Source
var InstallGitHubCommand = cmd.Command{
	Name:    "github",
	Aliases: []string{"gh"},
	Summary: "install GitHub hosted packages",
	Description: "Allows you to install packages from GitHub.\n\n" +
		"Expects `{org}/{repo}`, i.e. if you want to install the package at https://github.com/internet-computer/testing.mo you will have to pass `internet-computer/testing.mo` to the first argument.\n\n" +
		"Instead of specifying a specific version, `latest` can be used.",
	Args: []string{"url", "version"},
	Options: []cmd.Option{
		{
			Name:     "name",
			Summary:  "package name",
			HasValue: true,
		},
	},
	Method: func(args []string, options map[string]string) error {
		url := args[0]
		version := args[1]
		if version == "latest" {
			resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/releases", url))
			if err != nil {
				return NewInstallError(err)
			}
			data, err := io.ReadAll(resp.Body)
			if err != nil {
				return NewInstallError(err)
			}
			var releases []github.Release
			if err := json.Unmarshal(data, &releases); err != nil {
				return NewInstallError(err)
			}
			if len(releases) == 0 {
				return NewInstallError(github.NewReleasesNotFoundErrors(url))
			}
			version = releases[0].TagName
		}

		info := config.PackageInfoRemote{
			Repository: fmt.Sprintf("https://github.com/%s", url),
			Version:    version,
		}

		name, ok := options["name"]
		if !ok {

			name := url[strings.LastIndex(url, "/")+1:]
			if cmd.AskForConfirmation(fmt.Sprintf("Do you want to rename the package name %q?", name)) {
				info.Name = strings.TrimSpace(cmd.Ask("New name"))
			} else {
				info.Name = name
			}
		} else {
			info.Name = name
		}

		state, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewInstallError(err)
		}

		if err := info.Download(); err != nil {
			return NewInstallError(err)
		}

		if raw, err := os.ReadFile(fmt.Sprintf("%s/vessel.dhall", info.RelativePath())); err == nil {
			manifest, err := vessel.NewManifest(raw)
			if err != nil {
				return NewInstallError(err)
			}
			info.Dependencies = manifest.Dependencies
			if len(manifest.Dependencies) != 0 {
				packageSet, err := vessel.LoadPackageSet(fmt.Sprintf("%s/package-set.dhall", info.RelativePath()))
				if err != nil {
					return NewInstallError(err)
				}
				packages, err := packageSet.Filter(manifest.Dependencies)
				if err != nil {
					return NewInstallError(err)
				}
				if err := state.AddPackage(info, packages.Oko()...); err != nil {
					return NewInstallError(err)
				}
			} else {
				if err := state.AddPackage(info); err != nil {
					return nil
				}
			}
			if err := state.Save("./oko.json"); err != nil {
				return NewInstallError(err)
			}
			return nil
		}

		if raw, err := os.ReadFile(fmt.Sprintf("%s/oko.json", info.RelativePath())); err == nil {
			pkg, err := config.NewPackageConfig(raw)
			if err != nil {
				return NewInstallError(err)
			}
			if err := state.LoadState(config.NewPackageState(pkg)); err != nil {
				return NewInstallError(err)
			}
			if err := state.Save("./oko.json"); err != nil {
				return NewInstallError(err)
			}
			return nil
		}

		if _, err := os.Stat(fmt.Sprintf("%s/src", info.RelativePath())); err != nil {
			return NewInstallError(err)
		}
		if err := state.AddPackage(info); err != nil {
			return NewInstallError(err)
		}
		if err := state.Save("./oko.json"); err != nil {
			return NewInstallError(err)
		}
		return nil
	},
}
View Source
var InstallLocalCommand = cmd.Command{
	Name:        "local",
	Aliases:     []string{"l"},
	Summary:     "install local packages",
	Description: `Allows you to link local packages as dependencies.`,
	Args:        []string{"path"},
	Options: []cmd.Option{
		{
			Name:     "name",
			Summary:  "package name",
			HasValue: true,
		},
	},
	Method: func(args []string, options map[string]string) error {
		path := args[0]
		if _, err := os.Stat(path); err != nil {
			return NewInstallError(err)
		}
		info := config.PackageInfoLocal{
			Path: path,
		}

		name, ok := options["name"]
		if !ok {

			name := path[strings.LastIndex(path, "/")+1:]
			if cmd.AskForConfirmation(fmt.Sprintf("Do you want to rename the package name %q?", name)) {
				info.Name = strings.TrimSpace(cmd.Ask("New name"))
			} else {
				info.Name = name
			}
		} else {
			info.Name = name
		}

		state, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewInstallError(err)
		}
		if err := state.AddLocalPackage(info); err != nil {
			return NewInstallError(err)
		}
		if err := state.Save("./oko.json"); err != nil {
			return NewInstallError(err)
		}
		return nil
	},
}
View Source
var MigrateCommand = cmd.Command{
	Name:        "migrate",
	Summary:     "migrate Vessel packages",
	Description: `Allows you to migrate Vessel config files to Oko.`,
	Options: []cmd.Option{
		{
			Name:     "delete",
			HasValue: false,
		},
		{
			Name:     "keep",
			HasValue: false,
		},
	},
	Method: func(_ []string, options map[string]string) error {
		if 2 <= len(options) {
			return NewMigrateError(NewOptionsError("can not use both `delete` and `keep` at the same time"))
		}
		if _, err := config.LoadPackageState("./oko.json"); err == nil {
			return NewMigrateError(err)
		}

		manifest, err := vessel.LoadManifest("./vessel.dhall")
		if err != nil {
			return NewMigrateError(err)
		}
		packageSet, err := vessel.LoadPackageSet("./package-set.dhall")
		if err != nil {
			return NewMigrateError(err)
		}

		packages, err := packageSet.Filter(manifest.Dependencies)
		if err != nil {
			return NewMigrateError(err)
		}

		if err := manifest.Save("./oko.json", packages); err != nil {
			return NewMigrateError(err)
		}

		if _, ok := options["keep"]; ok {
			return nil
		}
		if _, ok := options["delete"]; ok || cmd.AskForConfirmation("Do you want to delete the `vessel.dhall` and `package-set.dhall` file?") {
			if err := os.Remove("./vessel.dhall"); err != nil {
				return NewMigrateError(err)
			}
			if err := os.Remove("./package-set.dhall"); err != nil {
				return NewMigrateError(err)
			}
		}
		return nil
	},
}
View Source
var RemoveCommand = cmd.Command{
	Name:        "remove",
	Aliases:     []string{"r"},
	Summary:     "remove a package",
	Description: `Allows you to remove packages by name.`,
	Args:        []string{"name"},
	Method: func(args []string, _ map[string]string) error {
		state, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewRemoveError(err)
		}

		name := args[0]
		if err := state.RemoveLocalPackage(name); err == nil {
			if err := state.Save("./oko.json"); err != nil {
				return NewRemoveError(err)
			}
			return nil
		}

		if err := state.RemovePackage(name); err != nil {
			return NewRemoveError(err)
		}
		if err := state.Save("./oko.json"); err != nil {
			return NewRemoveError(err)
		}
		return nil
	},
}
View Source
var SourcesCommand = cmd.Command{
	Name:    "sources",
	Summary: "prints moc package sources",
	Method: func(_ []string, _ map[string]string) error {
		state, err := config.LoadPackageState("./oko.json")
		if err != nil {
			return NewSourcesError(err)
		}

		var sources []string
		for _, dep := range state.Dependencies {
			sources = append(sources, fmt.Sprintf(
				"--package %s %s/src", dep.Name, dep.RelativePath(),
			))
			for _, name := range dep.AlternativeNames {
				sources = append(sources, fmt.Sprintf(
					"--package %s %s/src", name, dep.RelativePath(),
				))
			}
		}
		for _, dep := range state.TransitiveDependencies {
			sources = append(sources, fmt.Sprintf(
				"--package %s %s/src", dep.Name, dep.RelativePath(),
			))
			for _, name := range dep.AlternativeNames {
				sources = append(sources, fmt.Sprintf(
					"--package %s %s/src", name, dep.RelativePath(),
				))
			}
		}
		for _, dep := range state.LocalDependencies {
			sources = append(sources, fmt.Sprintf(
				"--package %s %s", dep.Name, dep.Path,
			))
		}
		fmt.Print(strings.Join(sources, " "))
		return nil
	},
}

Functions

This section is empty.

Types

type BinError

type BinError struct {
	Err error
}

func NewBinError

func NewBinError(err error) *BinError

func (BinError) Error

func (e BinError) Error() string

type CompilerVersionNotFoundError

type CompilerVersionNotFoundError struct{}

func NewCompilerVersionNotFoundError

func NewCompilerVersionNotFoundError() *CompilerVersionNotFoundError

func (CompilerVersionNotFoundError) Error

type DownloadError

type DownloadError struct {
	Err error
}

func NewDownloadError

func NewDownloadError(err error) *DownloadError

func (DownloadError) Error

func (e DownloadError) Error() string

type InitError

type InitError struct {
	Err error
}

func NewInitError

func NewInitError(err error) *InitError

func (InitError) Error

func (e InitError) Error() string

type InstallError

type InstallError struct {
	Err error
}

func NewInstallError

func NewInstallError(err error) *InstallError

func (InstallError) Error

func (e InstallError) Error() string

type MigrateError

type MigrateError struct {
	Err error
}

func NewMigrateError

func NewMigrateError(err error) *MigrateError

func (MigrateError) Error

func (e MigrateError) Error() string

type OptionsError

type OptionsError struct {
	Message string
}

func NewOptionsError

func NewOptionsError(msg string) *OptionsError

func (OptionsError) Error

func (e OptionsError) Error() string

type PathNotFoundError

type PathNotFoundError struct {
	Path string
}

func NewPathNotFoundError

func NewPathNotFoundError(path string) *PathNotFoundError

func (PathNotFoundError) Error

func (e PathNotFoundError) Error() string

type RemoveError

type RemoveError struct {
	Err error
}

func NewRemoveError

func NewRemoveError(err error) *RemoveError

func (RemoveError) Error

func (e RemoveError) Error() string

type SourcesError

type SourcesError struct {
	Err error
}

func NewSourcesError

func NewSourcesError(err error) *SourcesError

func (SourcesError) Error

func (e SourcesError) Error() string

type UnsupportedRuntimeErrors

type UnsupportedRuntimeErrors struct {
	GOOS string
}

func NewUnsupportedRuntimeErrors

func NewUnsupportedRuntimeErrors(goos string) *UnsupportedRuntimeErrors

func (UnsupportedRuntimeErrors) Error

func (e UnsupportedRuntimeErrors) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL