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

Initial pkgpatch check-in #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions cmd/pkgpatch/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"fmt"
"os"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/urfave/cli/v2"
)

func openRepo(rootFS billy.Filesystem, path string) (*git.Repository, error) {
dot, err := rootFS.Chroot(path + "/.git")
if err != nil {
return nil, err
}
storer := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
repo, err := git.Open(storer, rootFS)
if err != nil {
return nil, err
}

return repo, nil
}

func getRootPatch(rootFS billy.Filesystem) (string, error) {
// Open the git repository
repo, err := openRepo(rootFS, ".")
if err != nil {
return "", err
}

// Check if there are any changes
wt, err := repo.Worktree()
if err != nil {
return "", err
}

status, err := wt.Status()
if err != nil {
return "", err
}

return status.String(), nil
}

func getTarballPatch(rootFS billy.Filesystem) (string, error) {
// Open the git repository
repo, err := openRepo(rootFS, "tarball")
if err != nil {
return "", err
}

// Check if there are any changes
wt, err := repo.Worktree()
if err != nil {
return "", err
}

status, err := wt.Status()
if err != nil {
return "", err
}

return status.String(), nil
}

func generate(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return cli.Exit("usage: pkgpatch generate MESSAGE", 1)
}
msg := ctx.Args().First()
fmt.Println(msg)

// Verify that this is ran from a .pkgpatchroot directory
currentDir, err := os.Getwd()
if err != nil {
return err
}

if _, err := os.Stat(".pkgpatchroot"); os.IsNotExist(err) {
return fmt.Errorf("not a .pkgpatchroot directory")
}

// Open the root filesystem
rootFS := osfs.New(currentDir)

// Get the root patch
rootPatch, err := getRootPatch(rootFS)
if err != nil {
return err
}
fmt.Println(rootPatch)

// Get the tarball patch
if _, err := rootFS.Stat("tarball"); err == nil {
tarballPatch, err := getTarballPatch(rootFS)
if err != nil {
return err
}
fmt.Println(tarballPatch)
}

return nil
}
28 changes: 28 additions & 0 deletions cmd/pkgpatch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"log/slog"
"os"

"github.com/openela/mothership/base"
"github.com/urfave/cli/v2"
)

func main() {
app := &cli.App{
Name: "pkgpatch",
Commands: []*cli.Command{
{
Name: "open",
Usage: "open an entry for patching",
Action: open,
},
// todo(mustafa): finish the "generate" command
},
Flags: base.WithFlags(),
}

if err := app.Run(os.Args); err != nil {
slog.Error("Could not run pkgpatch", "err", err)
}
}
Loading