From f14cd6aed69eb40d4a6d520372c4ee327888113b Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Thu, 2 Jun 2022 19:56:21 +0800 Subject: [PATCH] feat(lang): Support git_config rule (#235) Signed-off-by: Ce Gao --- pkg/lang/frontend/starlark/const.go | 1 + pkg/lang/frontend/starlark/rules.go | 36 +++++++++++++++++++++++- pkg/lang/ir/compile.go | 6 +++- pkg/lang/ir/git.go | 43 +++++++++++++++++++++++++++++ pkg/lang/ir/interface.go | 9 ++++++ pkg/lang/ir/types.go | 7 +++++ 6 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 pkg/lang/ir/git.go diff --git a/pkg/lang/frontend/starlark/const.go b/pkg/lang/frontend/starlark/const.go index 533e10c8b..7fd0590e8 100644 --- a/pkg/lang/frontend/starlark/const.go +++ b/pkg/lang/frontend/starlark/const.go @@ -25,4 +25,5 @@ const ( ruleShell = "shell" ruleJupyter = "jupyter" ruleRun = "run" + ruleGitConfig = "git_config" ) diff --git a/pkg/lang/frontend/starlark/rules.go b/pkg/lang/frontend/starlark/rules.go index ca0710266..3b65763a5 100644 --- a/pkg/lang/frontend/starlark/rules.go +++ b/pkg/lang/frontend/starlark/rules.go @@ -40,6 +40,7 @@ func registerenvdRules() { starlark.Universe[ruleShell] = starlark.NewBuiltin(ruleShell, ruleFuncShell) starlark.Universe[ruleJupyter] = starlark.NewBuiltin(ruleJupyter, ruleFuncJupyter) starlark.Universe[ruleRun] = starlark.NewBuiltin(ruleRun, ruleFuncRun) + starlark.Universe[ruleGitConfig] = starlark.NewBuiltin(ruleGitConfig, ruleFuncGitConfig) } func ruleFuncBase(thread *starlark.Thread, _ *starlark.Builtin, @@ -271,7 +272,7 @@ func ruleFuncRun(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var commands *starlark.List - if err := starlark.UnpackArgs(rulePyPIPackage, + if err := starlark.UnpackArgs(ruleRun, args, kwargs, "commands?", &commands); err != nil { return nil, err } @@ -290,3 +291,36 @@ func ruleFuncRun(thread *starlark.Thread, _ *starlark.Builtin, return starlark.None, nil } + +func ruleFuncGitConfig(thread *starlark.Thread, _ *starlark.Builtin, + args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var name, email, editor starlark.String + + if err := starlark.UnpackArgs(ruleGitConfig, + args, kwargs, "name?", &name, "email?", &email, "editor?", &editor); err != nil { + return nil, err + } + + nameStr := "" + if name != starlark.String("") { + nameStr = name.GoString() + } + + emailStr := "" + if email != starlark.String("") { + nameStr = email.GoString() + } + + editorStr := "" + if editor != starlark.String("") { + editorStr = editor.GoString() + } + + logger.Debugf("rule `%s` is invoked, name=%s, email=%s, editor=%s", + ruleGitConfig, nameStr, emailStr, editorStr) + if err := ir.Git(nameStr, emailStr, editorStr); err != nil { + return nil, err + } + + return starlark.None, nil +} diff --git a/pkg/lang/ir/compile.go b/pkg/lang/ir/compile.go index d5e841ab2..cd79ecc4a 100644 --- a/pkg/lang/ir/compile.go +++ b/pkg/lang/ir/compile.go @@ -141,6 +141,10 @@ func (g Graph) Compile() (llb.State, error) { // TODO(gaocegege): Support order-based exec. run := g.compileRun(merged) + finalStage, err := g.compileGit(run) + if err != nil { + return llb.State{}, errors.Wrap(err, "failed to compile git") + } g.Writer.Finish() - return run, nil + return finalStage, nil } diff --git a/pkg/lang/ir/git.go b/pkg/lang/ir/git.go new file mode 100644 index 000000000..485cca58b --- /dev/null +++ b/pkg/lang/ir/git.go @@ -0,0 +1,43 @@ +// 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 ir + +import ( + "fmt" + + "github.com/moby/buildkit/client/llb" +) + +const ( + templateGitConfig = ` +[user] + email = %s + name = %s +[core] + editor = %s + +` +) + +func (g *Graph) compileGit(root llb.State) (llb.State, error) { + if g.GitConfig == nil { + return root, nil + } + content := fmt.Sprintf(templateGitConfig, g.GitConfig.Email, g.GitConfig.Name, g.GitConfig.Editor) + installPath := "/home/envd/.gitconfig" + gitStage := root.File(llb.Mkfile(installPath, + 0644, []byte(content), llb.WithUIDGID(defaultUID, defaultGID))) + return gitStage, nil +} diff --git a/pkg/lang/ir/interface.go b/pkg/lang/ir/interface.go index 0ced54072..5f8cd8ce6 100644 --- a/pkg/lang/ir/interface.go +++ b/pkg/lang/ir/interface.go @@ -97,3 +97,12 @@ func Run(commands []string) error { DefaultGraph.Exec = commands return nil } + +func Git(name, email, editor string) error { + DefaultGraph.GitConfig = &GitConfig{ + Name: name, + Email: email, + Editor: editor, + } + return nil +} diff --git a/pkg/lang/ir/types.go b/pkg/lang/ir/types.go index 4fa24ed0e..9b1ffac4d 100644 --- a/pkg/lang/ir/types.go +++ b/pkg/lang/ir/types.go @@ -41,11 +41,18 @@ type Graph struct { Exec []string *JupyterConfig + *GitConfig Writer compileui.Writer CachePrefix string } +type GitConfig struct { + Name string + Email string + Editor string +} + type JupyterConfig struct { Password string Port int64