func (p *Strategy) Run(state State, version JujuVersionValidator, origin Origin) (DownloadResult, bool, Origin, error) {
	p.logger.Tracef("Run %+v", origin)
	ch, err := state.PrepareCharmUpload(p.charmURL)
	if err != nil {
		return DownloadResult{}, false, origin, errors.Trace(err)
	}

	seriesOrigin := origin
	seriesOrigin.Platform, err = p.normalizePlatform(origin.Platform)
	if err != nil {
		return DownloadResult{}, false, origin, errors.Trace(err)
	}

	// Charm is already in state, so we can exit out early.
	if ch.IsUploaded() {
		origin, err := p.store.DownloadOrigin(p.charmURL, seriesOrigin)
		if err != nil {
			return DownloadResult{}, false, Origin{}, errors.Trace(err)
		}

		p.logger.Debugf("Reusing charm: already uploaded to controller with origin %v", origin)
		return DownloadResult{}, true, origin, nil
	}

	p.logger.Debugf("Downloading charm %q: %v", p.charmURL, seriesOrigin)

	// Get the charm and its information from the store.
	file, err := ioutil.TempFile("", p.charmURL.Name)
	if err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}

	p.deferFunc(func() error {
		_ = file.Close()
		return nil
	})
	p.deferFunc(func() error {
		_ = os.Remove(file.Name())
		return nil
	})

	archive, checksum, downloadOrigin, err := p.store.Download(p.charmURL, file.Name(), seriesOrigin)
	if err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}

	if err := version.Validate(archive.Meta()); err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}

	// Validate the charm lxd profile once we've downloaded it.
	if err := lxdprofile.ValidateLXDProfile(makeStoreCharmLXDProfiler(archive)); err != nil && !p.force {
		return DownloadResult{}, false, Origin{}, errors.Annotate(err, "cannot add charm")
	}

	result, err := p.downloadResult(file.Name(), checksum)
	if err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}

	return DownloadResult{
		Charm:  archive,
		Data:   result.Data,
		Size:   result.Size,
		SHA256: result.SHA256,
	}, false, downloadOrigin, nil
}
// Run the download procedure against the supplied store adapter.
// Includes downloading the blob to a temp file and validating the contents
// of the charm.Meta and LXD profile data.
Run the download procedure against the supplied store adapter.
Includes downloading the blob to a temp file and validating the contents
of the charm.Meta and LXD profile data.
// Run the download procedure against the supplied store adapter . // Includes downloading the blob to a temp file and validating the contents // of the charm . Meta and LXD profile data .
func (p *Strategy) Run(state State, version JujuVersionValidator, origin Origin) (DownloadResult, bool, Origin, error) {
	p.logger.Tracef("Run %+v", origin)
	ch, err := state.PrepareCharmUpload(p.charmURL)
	if err != nil {
		return DownloadResult{}, false, origin, errors.Trace(err)
	}
	seriesOrigin := origin
	seriesOrigin.Platform, err = p.normalizePlatform(origin.Platform)
	if err != nil {
		return DownloadResult{}, false, origin, errors.Trace(err)
	}
	if ch.IsUploaded() {
		origin, err := p.store.DownloadOrigin(p.charmURL, seriesOrigin)
		if err != nil {
			return DownloadResult{}, false, Origin{}, errors.Trace(err)
		}
		p.logger.Debugf("Reusing charm: already uploaded to controller with origin %v", origin)
		return DownloadResult{}, true, origin, nil
	}
	p.logger.Debugf("Downloading charm %q: %v", p.charmURL, seriesOrigin)
	file, err := ioutil.TempFile("", p.charmURL.Name)
	if err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}
	p.deferFunc(func() error {
		_ = file.Close()
		return nil
	})
	p.deferFunc(func() error {
		_ = os.Remove(file.Name())
		return nil
	})
	archive, checksum, downloadOrigin, err := p.store.Download(p.charmURL, file.Name(), seriesOrigin)
	if err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}
	if err := version.Validate(archive.Meta()); err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}
	if err := lxdprofile.ValidateLXDProfile(makeStoreCharmLXDProfiler(archive)); err != nil && !p.force {
		return DownloadResult{}, false, Origin{}, errors.Annotate(err, "cannot add charm")
	}
	result, err := p.downloadResult(file.Name(), checksum)
	if err != nil {
		return DownloadResult{}, false, Origin{}, errors.Trace(err)
	}
	return DownloadResult{
		Charm:  archive,
		Data:   result.Data,
		Size:   result.Size,
		SHA256: result.SHA256,
	}, false, downloadOrigin, nil
}
func ( p * Strategy ) Run ( state State , version JujuVersionValidator , origin Origin ) ( DownloadResult , bool , Origin , error ) { p . logger . Tracef ( " " , origin ) ch , err := state . PrepareCharmUpload ( p . charmURL ) if err != nil { return DownloadResult { } , false , origin , errors . Trace ( err ) } seriesOrigin := origin seriesOrigin . Platform , err = p . normalizePlatform ( origin . Platform ) if err != nil { return DownloadResult { } , false , origin , errors . Trace ( err ) } if ch . IsUploaded ( ) { origin , err := p . store . DownloadOrigin ( p . charmURL , seriesOrigin ) if err != nil { return DownloadResult { } , false , Origin { } , errors . Trace ( err ) } p . logger . Debugf ( " " , origin ) return DownloadResult { } , true , origin , nil } p . logger . Debugf ( " " , p . charmURL , seriesOrigin ) file , err := ioutil . TempFile ( " " , p . charmURL . Name ) if err != nil { return DownloadResult { } , false , Origin { } , errors . Trace ( err ) } p . deferFunc ( func ( ) error { _ = file . Close ( ) return nil } ) p . deferFunc ( func ( ) error { _ = os . Remove ( file . Name ( ) ) return nil } ) archive , checksum , downloadOrigin , err := p . store . Download ( p . charmURL , file . Name ( ) , seriesOrigin ) if err != nil { return DownloadResult { } , false , Origin { } , errors . Trace ( err ) } if err := version . Validate ( archive . Meta ( ) ) ; err != nil { return DownloadResult { } , false , Origin { } , errors . Trace ( err ) } if err := lxdprofile . ValidateLXDProfile ( makeStoreCharmLXDProfiler ( archive ) ) ; err != nil && ! p . force { return DownloadResult { } , false , Origin { } , errors . Annotate ( err , " " ) } result , err := p . downloadResult ( file . Name ( ) , checksum ) if err != nil { return DownloadResult { } , false , Origin { } , errors . Trace ( err ) } return DownloadResult { Charm : archive , Data : result . Data , Size : result . Size , SHA256 : result . SHA256 , } , false , downloadOrigin , nil }
Run the download procedure against the supplied store adapter.
Run the download procedure against the supplied store adapter .
// Charm is already in state, so we can exit out early. // Get the charm and its information from the store. // Validate the charm lxd profile once we've downloaded it.