github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/s3/awssession.go (about)

     1  package s3
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/aws/credentials"
     6  	"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
     7  	"github.com/aws/aws-sdk-go/aws/session"
     8  )
     9  
    10  type SessionBuilder interface {
    11  	Config(*aws.Config) SessionBuilder
    12  	Profile(string) SessionBuilder
    13  	Options(*session.Options) SessionBuilder
    14  	Endpoint(string) SessionBuilder
    15  	S3ForcePathStyle(bool) SessionBuilder
    16  	Build() *session.Session
    17  }
    18  
    19  type sessionBuilder struct {
    20  	awsConfig      *aws.Config
    21  	profile        string
    22  	options        *session.Options
    23  	endpoint       *string
    24  	forcePathStyle *bool
    25  }
    26  
    27  func (sb *sessionBuilder) Config(c *aws.Config) SessionBuilder {
    28  	sb.awsConfig = c
    29  	return sb
    30  }
    31  
    32  func (sb *sessionBuilder) Profile(p string) SessionBuilder {
    33  	sb.profile = p
    34  	return sb
    35  }
    36  
    37  func (sb *sessionBuilder) Endpoint(e string) SessionBuilder {
    38  	sb.endpoint = aws.String(e)
    39  	return sb
    40  }
    41  
    42  func (sb *sessionBuilder) S3ForcePathStyle(b bool) SessionBuilder {
    43  	sb.forcePathStyle = aws.Bool(b)
    44  	return sb
    45  }
    46  
    47  func (sb *sessionBuilder) Options(o *session.Options) SessionBuilder {
    48  	sb.options = o
    49  	return sb
    50  }
    51  
    52  func (sb *sessionBuilder) Build() *session.Session {
    53  	if sb.awsConfig == nil {
    54  		sb.awsConfig = aws.NewConfig()
    55  	}
    56  
    57  	if sb.endpoint != nil {
    58  		sb.awsConfig.Endpoint = sb.endpoint
    59  		sb.awsConfig.S3ForcePathStyle = sb.forcePathStyle
    60  	}
    61  
    62  	sb.awsConfig.Credentials = credentials.NewChainCredentials([]credentials.Provider{
    63  		&credentials.EnvProvider{},
    64  		&credentials.SharedCredentialsProvider{
    65  			Profile: sb.profile,
    66  		},
    67  	})
    68  
    69  	_, err := sb.awsConfig.Credentials.Get()
    70  	if err == nil {
    71  		return session.Must(session.NewSession(sb.awsConfig))
    72  	}
    73  	if sb.options == nil {
    74  		sb.options = &session.Options{
    75  			AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
    76  			SharedConfigState:       session.SharedConfigEnable,
    77  			Profile:                 sb.profile,
    78  		}
    79  	}
    80  
    81  	return session.Must(session.NewSessionWithOptions(*sb.options))
    82  }
    83  
    84  func newSessionBuilder() SessionBuilder {
    85  	return &sessionBuilder{}
    86  }