Skip to content

Commit

Permalink
feat(lang): Support git_config rule (#235)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed Jun 2, 2022
1 parent 8555bc6 commit f14cd6a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/lang/frontend/starlark/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ const (
ruleShell = "shell"
ruleJupyter = "jupyter"
ruleRun = "run"
ruleGitConfig = "git_config"
)
36 changes: 35 additions & 1 deletion pkg/lang/frontend/starlark/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
6 changes: 5 additions & 1 deletion pkg/lang/ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
43 changes: 43 additions & 0 deletions pkg/lang/ir/git.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f14cd6a

Please sign in to comment.