Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/sync-await.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"crypto/sha1"
9
"encoding/hex"
10
"fmt"
11
"os"
12
"time"
13
14
"github.com/spf13/cobra"
15
)
16
17
// awaitSyncCmd represents the awaitSync command
18
var awaitSyncCmd = &cobra.Command{
19
Use: "sync-await <name>",
20
Short: "Awaits an event triggered using gp sync-done",
21
Args: cobra.ExactArgs(1),
22
RunE: func(cmd *cobra.Command, args []string) error {
23
h := sha1.New()
24
h.Write([]byte(args[0]))
25
id := hex.EncodeToString(h.Sum(nil))
26
lockFile := fmt.Sprintf("/tmp/gp-%s.done", id)
27
28
ticker := time.NewTicker(1 * time.Second)
29
done := make(chan bool)
30
go func() {
31
for {
32
if _, err := os.Stat(lockFile); !os.IsNotExist(err) {
33
break
34
}
35
36
<-ticker.C
37
}
38
39
ticker.Stop()
40
done <- true
41
}()
42
43
select {
44
case <-cmd.Context().Done():
45
case <-done:
46
fmt.Printf("%s done\n", args[0])
47
}
48
49
return nil
50
},
51
}
52
53
func init() {
54
rootCmd.AddCommand(awaitSyncCmd)
55
}
56
57