-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (44 loc) · 841 Bytes
/
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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"github.com/montag451/go-pypi-mirror/cmd"
)
func printAvailableCommands() {
fmt.Println("Available commands:")
for _, name := range cmd.Names() {
fmt.Println(name)
}
}
func main() {
if len(os.Args) <= 1 {
printAvailableCommands()
os.Exit(1)
}
name := os.Args[1]
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error)
go func() {
defer close(errCh)
if err := cmd.Execute(ctx, name, os.Args[2:]); err != nil {
errCh <- fmt.Errorf("failed to execute command %q: %w", name, err)
}
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
select {
case err, ok := <-errCh:
if ok {
log.Fatal(err)
}
case <-sigCh:
cancel()
if err, ok := <-errCh; ok {
log.Fatal(err)
}
}
}