albatros/cmd/static.go

128 lines
2.4 KiB
Go

package cmd
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"gocloud.dev/blob"
_ "gocloud.dev/blob/s3blob"
)
type Platform struct {
OS string
Arch string
Root string
Files []string
}
func CollectPlatforms(path string) ([]Platform, error) {
var p []Platform
osDirList, err := os.ReadDir(path)
if err != nil {
return p, err
}
// Collect platforms
for _, osDir := range osDirList {
archDirList, err := os.ReadDir(filepath.Join(path, osDir.Name()))
if err != nil {
return p, err
}
for _, archDir := range archDirList {
root := filepath.Join(path, osDir.Name(), archDir.Name())
files, err := os.ReadDir(root)
var filenames []string
for _, f := range files {
filenames = append(filenames, f.Name())
}
if err != nil {
return p, err
}
plat := Platform {
OS: osDir.Name(),
Arch: archDir.Name(),
Root: root,
Files: filenames,
}
p = append(p, plat)
}
}
return p, nil
}
type Artifact struct {
Name string
Tag string
Platforms []Platform
}
func NewArtifact(nametag, path string) (Artifact, error) {
ntspl := strings.Split(nametag, ":")
if len(ntspl) != 2 {
return Artifact{}, errors.New("nametag must be of the form 'name:tag'")
}
ar := Artifact{
Name: ntspl[0],
Tag: ntspl[1],
}
plat, err := CollectPlatforms(path)
if err != nil {
return ar, err
}
ar.Platforms = plat
return ar, nil
}
var staticCmd = &cobra.Command{
Use: "static",
Short: "Manage static artifacts",
Long: "There are many ways to ship software, one is simply to publish a bunch of files on a mirror.",
}
var tag string
var publishCmd = &cobra.Command{
Use: "publish [folder] [remote]", // https://gocloud.dev/howto/blob/#s3-compatible
Short: "Publish a static artifact",
Long: "Sending logic for a static artifact",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
localFolder := args[0]
remoteUrl := args[1]
art, err := NewArtifact(tag, localFolder)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%#v\n", art)
bucket, err := blob.OpenBucket(context.Background(), remoteUrl)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer bucket.Close()
},
}
func init() {
publishCmd.Flags().StringVarP(&tag, "tag", "t", "", "Tag of the project, eg. albatros:0.9")
publishCmd.MarkFlagRequired("tag")
staticCmd.AddCommand(publishCmd)
RootCmd.AddCommand(staticCmd)
}