Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Dec 21, 2023
0 parents commit 6e016c2
Show file tree
Hide file tree
Showing 69 changed files with 4,576 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: ci

on:
push:
branches:
- "*"
tags:
- 'v*'

jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: '^1.21'

- run: make release
env:
GH_PASSWORD: ${{ secrets.GITHUB_TOKEN }}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.build/
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PIPER = go run ./cmd/piper

DEBUG = 0
ifeq ($(DEBUG),1)
PIPER := $(PIPER) --log-level=debug
endif


build:
$(PIPER) do go build

release:
$(PIPER) do release
18 changes: 18 additions & 0 deletions cmd/piper/do.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/innoai-tech/infra/pkg/cli"

"github.com/octohelm/piper/pkg/engine"
"github.com/octohelm/piper/pkg/logutil"
)

func init() {
cli.AddTo(App, &Do{})
}

type Do struct {
cli.C
logutil.Logger
engine.Pipeline
}
18 changes: 18 additions & 0 deletions cmd/piper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"context"
"os"

"github.com/innoai-tech/infra/pkg/cli"

"github.com/octohelm/piper/internal/version"
)

var App = cli.NewApp("piper", version.Version())

func main() {
if err := cli.Execute(context.Background(), App, os.Args[1:]); err != nil {
os.Exit(1)
}
}
3 changes: 3 additions & 0 deletions cue.mod/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gen/
pkg/
module.sum
5 changes: 5 additions & 0 deletions cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module: "github.com/octohelm/piper"

require: {
"piper.octohelm.tech": "v0.0.0"
}
148 changes: 148 additions & 0 deletions cuepkg/piper.octohelm.tech/github/release.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package github

import (
"path"
"strings"
"strconv"
"encoding/json"

"piper.octohelm.tech/http"
"piper.octohelm.tech/file"
"piper.octohelm.tech/client"
"piper.octohelm.tech/flow"
)

#GithubAPI: {
core: "https://api.github.com"
uploads: "https://uploads.github.com"
}

#Release: {
token: client.#Secret

owner: string
repo: string

name: string | *"latest"
notes: string | *""
prerelease: bool | *true
draft: bool | *false

assets: [...file.#File]

_client: #Client & {"token": token}

_get_or_create_release: {
_ret: flow.#First & {
tasks: [
_client.#Do & {
method: "GET"
url: "\(#GithubAPI.core)/repos/\(owner)/\(repo)/releases/tags/\(name)"
},
_client.#Do & {
"method": "POST"
"url": "\(#GithubAPI.core)/repos/\(owner)/\(repo)/releases"
"header": {
"Content-Type": "application/json"
}
"body": json.Marshal({
"tag_name": name
"name": name
"body": notes
"prerelease": prerelease
"draft": draft
})
},
]
}

id: _ret.result.data.id
}

_list_assets: {
_req: _client.#Do & {
method: "GET"
url: "\(#GithubAPI.core)/repos/\(owner)/\(repo)/releases/\(_get_or_create_release.id)/assets"
}

assets: {
for asset in _req.result.data {
"\(asset.name)": strconv.FormatFloat(asset.id, strings.ByteAt("f", 0), 0, 64)
}
}
}

_upload_assets: flow.#All & {
tasks: [
for f in assets {
let assetName = path.Base(f.filename)

flow.#All & {
tasks: [
// if asset name exists, delete first
if _list_assets.assets[assetName] != _|_ {
_client.#Do & {
"method": "DELETE"
"url": "\(#GithubAPI.core)/repos/\(owner)/\(repo)/releases/assets/\(_list_assets.assets[assetName])"
}
},
// then upload
_client.#Do & {
"method": "POST"
"url": "\(#GithubAPI.uploads)/repos/\(owner)/\(repo)/releases/\(_get_or_create_release.id)/assets"
"header": {
"Content-Type": "application/octet-stream"
}
"query": {
"name": "\(assetName)"
}
"body": f
},
]
}
},
]
}
result: _upload_assets.result
}

#Client: {
token: client.#Secret

_token: client.#ReadSecret & {
secret: token
}

#Do: {
method: string
url: string
body?: file.#StringOrFile
header: [Name=string]: string | [...string]
query: [Name=string]: string | [...string]

_default_header: {
"Accept": "application/vnd.github+json"
"Authorization": "Bearer \(_token.value)"
"X-GitHub-Api-Version": "2022-11-28"
}

_req: http.#Do & {
"method": method
"url": url
"header": {
for k, vv in header {
"\(k)": vv
}
for k, vv in _default_header if header[k] == _|_ {
"\(k)": vv
}
}
"query": query
if body != _|_ {
"body": body
}
}

result: _req.result
}
}
96 changes: 96 additions & 0 deletions cuepkg/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cuepkg

import (
"context"
"embed"
"github.com/octohelm/piper/pkg/engine/task"
"github.com/pkg/errors"
"io"
"io/fs"
"os"
"path/filepath"

"github.com/octohelm/cuemod/pkg/cuemod/stdlib"
"github.com/octohelm/unifs/pkg/filesystem"
)

//go:embed piper.octohelm.tech
var externalModules embed.FS

var (
PiperModule = "piper.octohelm.tech"
)

func RegistryCueStdlibs() error {
source, err := task.Factory.Sources(context.Background())
if err != nil {
return err
}

module, err := createModule(externalModules, filesystem.AsReadDirFS(source))
if err != nil {
return err
}

// ugly lock embed version
if err := registerStdlib(filesystem.AsReadDirFS(module), "v0.0.0", PiperModule); err != nil {
return err
}

return nil
}

func registerStdlib(fs fs.ReadDirFS, ver string, modules ...string) error {
stdlib.Register(fs, ver, modules...)
return nil
}

func createModule(otherFs ...fs.ReadDirFS) (filesystem.FileSystem, error) {
mfs := filesystem.NewMemFS()

ctx := context.Background()

for _, f := range otherFs {
if err := listFile(f, ".", func(filename string) error {
src, err := f.Open(filename)
if err != nil {
return errors.Wrap(err, "open source file failed")
}
defer src.Close()

if err := filesystem.MkdirAll(ctx, mfs, filepath.Dir(filename)); err != nil {
return err
}
dest, err := mfs.OpenFile(ctx, filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return errors.Wrap(err, "open dest file failed")
}
defer dest.Close()

if _, err := io.Copy(dest, src); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
}

return mfs, nil
}

func listFile(f fs.ReadDirFS, root string, each func(filename string) error) error {
return fs.WalkDir(f, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
rel := path
if root != "" && root != "." {
rel, _ = filepath.Rel(root, path)
}
return each(rel)
})
}
17 changes: 17 additions & 0 deletions cuepkg/pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cuepkg

import (
"github.com/octohelm/piper/pkg/wd"
"testing"
)

func TestCuePkg(t *testing.T) {
cuepkgs, err := createModule(externalModules)
if err != nil {
t.Fatal(err)
}

_ = wd.ListFile(cuepkgs, "", func(filename string) error {
return nil
})
}
Loading

0 comments on commit 6e016c2

Please sign in to comment.