github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/cmd/docker-app/pack.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/docker/app/internal/packager"
     9  	"github.com/docker/cli/cli"
    10  	"github.com/docker/cli/cli/command"
    11  	"github.com/spf13/cobra"
    12  	"golang.org/x/crypto/ssh/terminal"
    13  )
    14  
    15  var packOutputFile string
    16  
    17  func packCmd(dockerCli command.Cli) *cobra.Command {
    18  	cmd := &cobra.Command{
    19  		Use:   "pack [<app-name>] [-o output_file]",
    20  		Short: "Pack the application as a single file",
    21  		Args:  cli.RequiresMaxArgs(1),
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			app, err := packager.Extract(firstOrEmpty(args))
    24  			if err != nil {
    25  				return err
    26  			}
    27  			defer app.Cleanup()
    28  			var target io.Writer
    29  			if packOutputFile == "-" {
    30  				if terminal.IsTerminal(int(dockerCli.Out().FD())) {
    31  					return fmt.Errorf("Refusing to output to a terminal, use a shell redirect or the '-o' option")
    32  				}
    33  			} else {
    34  				target, err = os.Create(packOutputFile)
    35  				if err != nil {
    36  					return err
    37  				}
    38  			}
    39  			return packager.Pack(app.Path, target)
    40  		},
    41  	}
    42  	cmd.Flags().StringVarP(&packOutputFile, "output", "o", "-", "Output file (- for stdout)")
    43  	return cmd
    44  }