github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/version.go (about) 1 /* 2 * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "net/http" 22 "os" 23 "path" 24 "strings" 25 "time" 26 27 "github.com/fnproject/cli/client" 28 "github.com/urfave/cli" 29 ) 30 31 // Version of Fn CLI 32 var Version = "0.4.143" 33 34 // VersionCommand 35 func VersionCommand() cli.Command { 36 return cli.Command{ 37 Name: "version", 38 Usage: "Display CLI and server versions", 39 Description: "This is commands shows the latest client and server version.", 40 Action: versionCMD, 41 } 42 } 43 44 func versionCMD(c *cli.Context) error { 45 provider, err := client.CurrentProvider() 46 if err != nil { 47 return err 48 } 49 50 ver := getLatestVersion() 51 if ver == "" { 52 ver = "Client version: " + Version 53 } 54 fmt.Println(ver) 55 56 versionClient := provider.VersionClient() 57 v, err := versionClient.GetVersion(nil) 58 if err != nil { 59 fmt.Println("Server version: ", "?") 60 return nil 61 } 62 fmt.Println("Server version: ", v.Payload.Version) 63 return nil 64 } 65 66 // PrintLatestVersion to terminal 67 func PrintLatestVersion() { 68 v := getLatestVersion() 69 if v != "" { 70 fmt.Fprintln(os.Stderr, v) 71 } 72 } 73 74 func getLatestVersion() string { 75 base := "https://github.com/fnproject/cli/releases" 76 url := "" 77 c := http.Client{} 78 c.Timeout = time.Second * 3 79 c.CheckRedirect = func(req *http.Request, via []*http.Request) error { 80 url = req.URL.String() 81 return nil 82 } 83 r, err := c.Get(fmt.Sprintf("%s/latest", base)) 84 if err != nil { 85 return "" 86 } 87 defer r.Body.Close() 88 if !strings.Contains(url, base) { 89 return "" 90 } 91 if path.Base(url) != Version { 92 return fmt.Sprintf("Client version: %s is not latest: %s", Version, path.Base(url)) 93 } 94 return "Client version is latest version: " + path.Base(url) 95 }