bd7bd9b30650ca74cf4997a5e1b3d8b7bc6e5a6b — Louis Solofrizzo 7 months ago dbd9ba3 1.0
sdk, csc, api: Add more intelligent versionning in the project

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
6 files changed, 129 insertions(+), 1 deletions(-)

M CMakeLists.txt
M api/main.go
A csc/cmd/version.go
A defs/git/CMakeLists.txt
M sdk/CMakeLists.txt
M sdk/request.go
M CMakeLists.txt => CMakeLists.txt +1 -0
@@ 24,6 24,7 @@ ####
  add_subdirectory(defs/go)
  add_subdirectory(defs/ansible)
+ add_subdirectory(defs/git)
  
  ####
  # External contributions

M api/main.go => api/main.go +12 -1
@@ 1,6 1,7 @@ package main
  
  import (
+ 	"cisco/sdk"
  	"crypto/hmac"
  	"crypto/md5"
  	"crypto/rand"


@@ 82,9 83,15 @@ password := flag.String("password", "", "Admin password (SHA256)")
  	email := flag.String("email", "", "Admin email")
  	filename := flag.String("config", "/etc/cisco-api.conf", "The yaml configuration file")
+ 	version := flag.Bool("version", false, "Show version")
  
  	flag.Parse()
  
+ 	if *version {
+ 		fmt.Printf("%s-%s (%s)\n", cisco.GitTag(), cisco.GitRev(), cisco.GitBranch())
+ 		os.Exit(0)
+ 	}
+ 
  	ReadConfig(*filename)
  	InitDB()
  


@@ 139,7 146,11 @@ app := iris.New()
  
  	app.OnErrorCode(404, func(ctx iris.Context) {
- 		APIError(ctx, 404, "The route you asked could not be found. Is your client up to date?")
+ 		if ctx.GetHeader("X-Csc-Version") == cisco.GitTag() {
+ 			APIError(ctx, 404, "The route you asked for could not be found")
+ 		} else {
+ 			APIError(ctx, 404, "The route you asked for could not be found (Server Version: "+cisco.GitTag()+", Client Version: "+ctx.GetHeader("X-Csc-Version")+")")
+ 		}
  	})
  
  	InitInviteRoutes(app)

A csc/cmd/version.go => csc/cmd/version.go +19 -0
@@ 0,0 1,19 @@
+ package cmd
+ 
+ import (
+ 	"cisco/sdk"
+ 	"fmt"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var VersionRootCmd = &cobra.Command{
+ 	Use:   "version",
+ 	Short: "Display command line version",
+ 	Run: func(cmd *cobra.Command, args []string) {
+ 		fmt.Printf("%s-%s (%s)\n", cisco.GitTag(), cisco.GitRev(), cisco.GitBranch())
+ 	},
+ }
+ 
+ func init() {
+ 	RootCmd.AddCommand(VersionRootCmd)
+ }

A defs/git/CMakeLists.txt => defs/git/CMakeLists.txt +93 -0
@@ 0,0 1,93 @@
+ macro(get_git_infos)
+     execute_process(COMMAND git log --pretty=format:'%h' -n 1
+                 OUTPUT_VARIABLE GIT_REV
+                 ERROR_QUIET)
+ 
+     # Check whether we got any revision (which isn't
+     # always the case, e.g. when someone downloaded a zip
+     # file from Github instead of a checkout)
+     if ("${GIT_REV}" STREQUAL "")
+         set(GIT_REV "N/A")
+         set(GIT_DIFF "")
+         set(GIT_TAG "N/A")
+         set(GIT_BRANCH "N/A")
+     else()
+         execute_process(
+             COMMAND bash -c "git diff --quiet --exit-code || echo +"
+             OUTPUT_VARIABLE GIT_DIFF)
+         execute_process(
+             COMMAND git describe --abbrev=0
+             OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
+         execute_process(
+             COMMAND git rev-parse --abbrev-ref HEAD
+             OUTPUT_VARIABLE GIT_BRANCH)
+ 
+         execute_process(
+             COMMAND bash -c "git describe --exact-match --tags 2>/dev/null || git rev-parse --short HEAD"
+             OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
+ 
+         string(STRIP "${GIT_REV}" GIT_REV)
+         string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
+         string(STRIP "${GIT_DIFF}" GIT_DIFF)
+         string(STRIP "${GIT_TAG}" GIT_TAG)
+         string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
+     endif()
+ endmacro(get_git_infos)
+ 
+ function(generate_git_c_version NAME)
+     get_git_infos()
+ 
+     set(VERSION "const char *GIT_REV=\"${GIT_REV}${GIT_DIFF}\";
+     const char *GIT_TAG=\"${GIT_TAG}\";
+     const char *GIT_BRANCH=\"${GIT_BRANCH}\";
+     const char *GIT_VERSION=\"${GIT_VERSION}\";
+     ")
+ 
+     if(EXISTS ${NAME})
+         file(READ ${NAME} VERSION_)
+     else()
+         set(VERSION_ "")
+     endif()
+ 
+     if (NOT "${VERSION}" STREQUAL "${VERSION_}")
+         file(WRITE ${NAME} "${VERSION}")
+     endif()
+ endfunction(generate_git_c_version)
+ 
+ function(generate_git_go_version NAME PKG)
+     get_git_infos()
+ 
+     set(GO_VERSION "package ${PKG}
+ 
+     const GITREV     = \"${GIT_REV}${GIT_DIFF}\"
+     const GITTAG     = \"${GIT_TAG}\"
+     const GITBRANCH  = \"${GIT_BRANCH}\"
+     const GITVERSION = \"${GIT_VERSION}\"
+ 
+     func GitVersion() string {
+         return GITVERSION
+     }
+ 
+     func GitTag() string {
+         return GITTAG
+     }
+ 
+     func GitBranch() string {
+         return GITBRANCH
+     }
+ 
+     func GitRev() string {
+         return GITREV
+     }
+     ")
+ 
+     if(EXISTS ${NAME})
+         file(READ ${NAME} GO_VERSION_)
+     else()
+         set(GO_VERSION_ "")
+     endif()
+ 
+     if (NOT "${GO_VERSION}" STREQUAL "${GO_VERSION_}")
+         file(WRITE ${NAME} "${GO_VERSION}")
+     endif()
+ endfunction(generate_git_go_version)

M sdk/CMakeLists.txt => sdk/CMakeLists.txt +3 -0
@@ 1,5 1,8 @@+generate_git_go_version(version.go cisco)
+ 
  add_go_component(cisco-sdk
      config.go
      request.go
      invite.go
+     version.go
  )

M sdk/request.go => sdk/request.go +1 -0
@@ 295,6 295,7 @@ }
  
  	httpReq.Header.Add("Date", time.Now().String())
+ 	httpReq.Header.Add("X-Csc-Version", GitTag())
  
  	if route.Protected == true {
  		var toSign string