github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/cmd/laze/main.go (about)

     1  // Implements a laze scheduler.
     2  package main
     3  
     4  import (
     5  	"context"
     6  	"flag"
     7  	"fmt"
     8  	"net"
     9  	"net/url"
    10  	"os"
    11  
    12  	"github.com/emcfarlane/larking/builder"
    13  	_ "github.com/emcfarlane/larking/cmd/internal/bindings"
    14  	"github.com/pkg/browser"
    15  )
    16  
    17  var (
    18  	flagWeb = flag.Bool("web", false, "Opens in the browser")
    19  )
    20  
    21  func run(ctx context.Context) error {
    22  	flag.Parse()
    23  
    24  	args := flag.Args()
    25  	if len(args) < 1 {
    26  		return fmt.Errorf("missing label")
    27  	}
    28  
    29  	label := args[0]
    30  	//args = args[:len(args)-1]
    31  
    32  	a, err := builder.Build(ctx, label)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	if *flagWeb {
    38  		addr := "localhost:0"
    39  		ln, err := net.Listen("tcp", addr)
    40  		if err != nil {
    41  			return err
    42  		}
    43  		defer ln.Close()
    44  
    45  		u := url.URL{
    46  			Scheme: "http",
    47  			Host:   ln.Addr().String(),
    48  			Path:   "/graph/" + label,
    49  		}
    50  		go func() {
    51  			if err := browser.OpenURL(u.String()); err != nil {
    52  				fmt.Println(err)
    53  			}
    54  		}()
    55  
    56  		fmt.Println(ln.Addr())
    57  		return builder.Serve(ln)
    58  	}
    59  
    60  	//b.Run(ctx, a)
    61  
    62  	// Report error on failed actions.
    63  	if err := a.FailureErr(); err != nil {
    64  		return err
    65  	}
    66  	return nil
    67  }
    68  
    69  func main() {
    70  	ctx := context.Background()
    71  	if err := run(ctx); err != nil {
    72  		fmt.Println(err)
    73  		os.Exit(1)
    74  	}
    75  }