-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
96 lines (81 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/gcrane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/jonjohnsonjr/dagdotdev/internal/apk"
"github.com/jonjohnsonjr/dagdotdev/internal/explore"
"github.com/jonjohnsonjr/dagdotdev/internal/git"
)
var auth = flag.Bool("auth", false, "use docker credentials")
var verbose = flag.Bool("v", false, "verbose logs")
func main() {
flag.Parse()
if *verbose {
logs.Debug.SetOutput(os.Stderr)
}
if err := run(flag.Args()); err != nil {
log.Fatal(err)
}
}
func run(args []string) error {
if len(args) < 1 {
return fmt.Errorf("usage %s apk | %s oci", os.Args[0], os.Args[0])
}
switch args[0] {
case "apk":
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("listening on %s", port)
opt := []apk.Option{apk.WithUserAgent("dagdotdev")}
if *auth || os.Getenv("AUTH") == "keychain" {
opt = append(opt, apk.WithKeychain(gcrane.Keychain))
}
if cgid := os.Getenv("CHAINGUARD_IDENTITY"); cgid != "" {
cgauth := apk.NewChainguardIdentityAuth(cgid, "https://issuer.enforce.dev", "apk.cgr.dev")
opt = append(opt, apk.WithAuth(cgauth))
}
if eg := os.Getenv("EXAMPLES"); eg != "" {
opt = append(opt, apk.WithExamples(strings.Split(eg, ",")))
}
return http.ListenAndServe(fmt.Sprintf(":%s", port), apk.New(args[1:], opt...))
case "oci":
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("listening on %s", port)
opt := []explore.Option{explore.WithUserAgent("dagdotdev")}
kcs := []authn.Keychain{}
if cgid := os.Getenv("CHAINGUARD_IDENTITY"); cgid != "" {
log.Printf("saw CHAINGUARD_IDENTITY=%q", cgid)
cgauth := explore.NewChainguardIdentityAuth(cgid, "https://issuer.enforce.dev", "cgr.dev")
kcs = append(kcs, cgauth)
}
if *auth || os.Getenv("AUTH") == "keychain" {
kcs = append(kcs, gcrane.Keychain)
}
if len(kcs) != 0 {
opt = append(opt, explore.WithKeychain(authn.NewMultiKeychain(kcs...)))
}
return http.ListenAndServe(fmt.Sprintf(":%s", port), explore.New(opt...))
case "git":
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("listening on %s", port)
opt := []git.Option{git.WithUserAgent("dagdotdev")}
return http.ListenAndServe(fmt.Sprintf(":%s", port), git.New(args[1:], opt...))
default:
return fmt.Errorf("usage %s apk | %s oci", os.Args[0], os.Args[0])
}
}