aboutsummarylogtreecommitdiffstats
path: root/helper.sh
blob: fd8bea0426beda633275e153c55e6dfdd5d8f88f (plain)
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
#!/bin/bash 

# $1 = package name
# $2 = target directory path
# $3 = branch/commit/revision string, if empty then maste ist used
function go_get {
  if [[ $1 == github.com* ]]
  then 
    get_git $1 $2 $3
  elif [[ $1 == code.google.com* ]]
  then 
    get_hg $1 $2 $3
  elif [[ $1 == golang.org* ]]
  then 
    get_golang $1 $2 $3
  else
    go get $1
  fi
}

# $1 = git package
# $2 = target directory path
# $3 = branch/commit/revision string, if empty then maste ist used
function get_git {
  git clone https://$1 $2
  if [[ $3 != "master" ]] && [[ ${3:0} == commit* ]]
  then
    cd $2
    git checkout -q ${3:7}
  elif [[ $3 != "master" ]] && [[ ${3:0} == tag* ]]
  then
    cd $2
    git checkout -q tags/${3:4}
  else
    cd $2
    git checkout -q ${3:7}
  fi
}

# $1 = mercury package name
# $2 = target directory path
# $3 = branch/commit/revision string, if empty then maste ist used
function get_hg {
  if [[ $3 == "master" ]] || [[ $3 == "" ]]
  then
    hg clone https://$1 $2
  else
    hg clone https://$1 -r $3 $2
  fi
}

function get_golang {
  local package=$1
  local match="golang.org/x"
  local replace="github.com/golang"
  local result=""

  result=${package/$match/$replace}

  get_git $result $2 $3
}

# Read the .gopmfile file and clone the branch/commits of the depends
# $1 = .gopmfile file path
# $2 = target directory path
function get_gopm {
  local startStr=""
  local revStr=""

  while read line
  do
    if [[ $startStr == 'X' ]] && [[ $line == '' ]]
    then
      break
    elif [[ $startStr == 'X' ]]
    then
      IFS="=" read -a array <<< "$line"
      if [[ ${array[1]} != "" ]]
      then
        local revStr=${array[1]//\`}
        go_get ${array[0]} "$2/${array[0]}" $revStr
      else
        go_get ${array[0]} "$2/${array[0]}" master
      fi
    elif [[ $line == '[deps]' ]]
    then
      local startStr="X"
    fi
  done <$1
}