File: commandContext.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (107 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package repo

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/go-git/go-git/v5/plumbing"
)

type CommandContext interface {
	SDK() SDKRepository
	Spec() SpecRepository
	CreateReleaseBranch(version string) (string, error)
	CheckExternalChanges()
}

type commandContext struct {
	sdk  SDKRepository
	spec SpecRepository

	checkExternalChanges func(ref, newRef *plumbing.Reference, err error)
}

func (c *commandContext) CheckExternalChanges() {
	ref := c.spec.LastHead()
	newRef, err := c.spec.Head()
	c.checkExternalChanges(ref, newRef, err)
}

func checkExternalChangesPanic(ref, newRef *plumbing.Reference, err error) {
	if err != nil {
		log.Fatalf("Cannot get head ref of azure-rest-api-specs when checking external changes: %+v", err)
		return
	}
	if ref.Hash().String() != newRef.Hash().String() {
		log.Fatalf("External changes detected in azure-rest-api-specs. Command executed on %s, but now on %s", ref.Hash().String(), newRef.Hash().String())
	}
}

func checkExternalChangesWarning(ref, newRef *plumbing.Reference, err error) {
	if err != nil {
		log.Printf("[WARNING] Cannot get head ref of azure-rest-api-specs when checking external changes: %+v", err)
		return
	}
	if ref.Hash().String() != newRef.Hash().String() {
		log.Printf("[WARNING] External changes detected in azure-rest-api-specs. Command executed on %s, but now on %s", ref.Hash().String(), newRef.Hash().String())
	}
}

func (c *commandContext) CreateReleaseBranch(version string) (string, error) {
	// append a time in long to avoid collision of branch names
	releaseBranchName := fmt.Sprintf(releaseBranchNamePattern, version, time.Now().Unix())

	log.Printf("Checking out to %s", plumbing.NewBranchReferenceName(releaseBranchName))
	if err := c.SDK().Checkout(&CheckoutOptions{
		Branch: plumbing.NewBranchReferenceName(releaseBranchName),
		Create: true,
	}); err != nil {
		return "", err
	}

	return releaseBranchName, nil

}

func (c *commandContext) SDK() SDKRepository {
	return c.sdk
}

func (c *commandContext) Spec() SpecRepository {
	return c.spec
}

func NewCommandContext(sdkPath, specPath string, panicWhenDetectExternalChanges bool) (CommandContext, error) {
	sdkRepo, err := OpenSDKRepository(sdkPath)
	if err != nil {
		return nil, err
	}
	specRepo, err := OpenSpecRepository(specPath)
	if err != nil {
		return nil, err
	}
	ctx := commandContext{
		sdk:  sdkRepo,
		spec: specRepo,
	}
	if panicWhenDetectExternalChanges {
		ctx.checkExternalChanges = checkExternalChangesPanic
	} else {
		ctx.checkExternalChanges = checkExternalChangesWarning
	}
	return &ctx, nil
}

func TempDir() string {
	return filepath.Join(os.TempDir(), fmt.Sprintf("generator-%v", time.Now().Unix()))
}

const (
	releaseBranchNamePattern = "release-%s-%v"
)