github.com/iasthc/atlas/cmd/atlas@v0.0.0-20230523071841-73246df3f88d/main.go (about) 1 // Copyright 2021-present The Atlas Authors. All rights reserved. 2 // This source code is licensed under the Apache 2.0 license found 3 // in the LICENSE file in the root directory of this source tree. 4 5 package main 6 7 import ( 8 "bytes" 9 "context" 10 "fmt" 11 "os" 12 "os/signal" 13 "time" 14 15 "github.com/iasthc/atlas/cmd/atlas/internal/cmdapi" 16 "github.com/iasthc/atlas/cmd/atlas/internal/cmdapi/vercheck" 17 _ "github.com/iasthc/atlas/cmd/atlas/internal/docker" 18 _ "github.com/iasthc/atlas/sql/mysql" 19 _ "github.com/iasthc/atlas/sql/mysql/mysqlcheck" 20 _ "github.com/iasthc/atlas/sql/postgres" 21 _ "github.com/iasthc/atlas/sql/postgres/postgrescheck" 22 _ "github.com/iasthc/atlas/sql/sqlite" 23 _ "github.com/iasthc/atlas/sql/sqlite/sqlitecheck" 24 "github.com/mitchellh/go-homedir" 25 "golang.org/x/mod/semver" 26 27 _ "github.com/go-sql-driver/mysql" 28 _ "github.com/lib/pq" 29 _ "github.com/mattn/go-sqlite3" 30 ) 31 32 func main() { 33 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) 34 cmdapi.Root.SetOut(os.Stdout) 35 update := checkForUpdate() 36 err := cmdapi.Root.ExecuteContext(ctx) 37 if u := update(); u != "" { 38 fmt.Println(u) 39 } 40 if err != nil { 41 os.Exit(1) 42 } 43 } 44 45 const ( 46 // envNoUpdate when enabled it cancels checking for update 47 envNoUpdate = "ATLAS_NO_UPDATE_NOTIFIER" 48 vercheckURL = "https://vercheck.ariga.io" 49 versionFile = "~/.atlas/release.json" 50 ) 51 52 func noText() string { return "" } 53 54 // checkForUpdate checks for version updates and security advisories for Atlas. 55 func checkForUpdate() func() string { 56 done := make(chan struct{}) 57 version := cmdapi.Version() 58 // Users may skip update checking behavior. 59 if v := os.Getenv(envNoUpdate); v != "" { 60 return noText 61 } 62 // Skip if the current binary version isn't set (dev mode). 63 if !semver.IsValid(version) { 64 return noText 65 } 66 path, err := homedir.Expand(versionFile) 67 if err != nil { 68 return noText 69 } 70 var message string 71 go func() { 72 defer close(done) 73 vc := vercheck.New(vercheckURL, path) 74 payload, err := vc.Check(version) 75 if err != nil { 76 return 77 } 78 var b bytes.Buffer 79 if err := vercheck.Notify.Execute(&b, payload); err != nil { 80 return 81 } 82 message = b.String() 83 }() 84 return func() string { 85 select { 86 case <-done: 87 case <-time.After(time.Millisecond * 500): 88 } 89 return message 90 } 91 }