func()

in pkg/tasks/crate/push.go [48:114]


func (t *PushTask) Run(ctx context.Context) error {
	// Create the reader
	reader, err := t.SpecReader(ctx)
	if err != nil {
		return fmt.Errorf("unable to open input specification reader: %w", err)
	}

	// Create output writer
	writer, err := t.OutputWriter(ctx)
	if err != nil {
		return fmt.Errorf("unable to open writer: %w", err)
	}

	// Decode Cratefile
	spec, err := cratefile.Parse(reader, "", "hcl")
	if err != nil {
		return fmt.Errorf("unable to parse input cratefile: %w", err)
	}

	// Prepare target resolver
	to, err := getResolver(t.Target, t.RegistryOpts)
	if err != nil {
		return fmt.Errorf("unable to initialize target: %w", err)
	}

	// Get absolute contxt path
	absContextPath, err := filepath.Abs(t.ContextPath)
	if err != nil {
		return fmt.Errorf("unable to get absolute context path: %w", err)
	}

	// Ensure the root actually exists
	fi, err := os.Stat(absContextPath)
	if err != nil {
		return fmt.Errorf("unable to check context path: %w", err)
	}
	if !fi.IsDir() {
		return fmt.Errorf("context path '%s' must be a directory", absContextPath)
	}

	log.For(ctx).Info("Building image from context ...", zap.String("context", absContextPath))

	// Prepare image from cratefile
	img, err := crate.Build(os.DirFS(absContextPath), spec)
	if err != nil {
		return fmt.Errorf("unable to generate image descriptor from specification: %w", err)
	}

	// Push the container
	m, err := crate.Push(ctx, to, t.Ref, img)
	if err != nil {
		return fmt.Errorf("unable to push crate to registry: %w", err)
	}

	if t.JSONOutput {
		if err := json.NewEncoder(writer).Encode(m); err != nil {
			return fmt.Errorf("unable to encode manifest as JSON: %w", err)
		}
	} else {
		fmt.Fprintf(writer, "Container successfully pushed !\n")
		fmt.Fprintf(writer, "Digest: %s\n", m.Digest.Hex())
		fmt.Fprintf(writer, "Size: %d\n", m.Size)
	}

	// No error
	return nil
}