external icon indicating copy to clipboard operation
external copied to clipboard

Add site with occ command

Open EWouters opened this issue 7 years ago • 15 comments

Hi, how can I add a site using the occ command?

php occ config:app:set external <name> --value VALUE

I'm not sure what to put in the <name> field.

Thanks

EWouters avatar Mar 19 '17 21:03 EWouters

php occ config:app:set external sites --value="XXX"

Replace XXX with the json encoded array of links. Or better don't for now, since it's not that easy. I will mostlikely add a new command to easily do that in the future.

nickvergessen avatar Mar 20 '17 01:03 nickvergessen

So here it is in bash:

do_add_external_site() {
  OCC="sudo -u www-data php /var/www/nextcloud/occ"
  NEW_SITE="[\"$1\",\"${2//\//\\/}\",\"$3\"]"
  CURRENT_SITES=$($OCC config:app:get external sites)
  if [ -z "$CURRENT_SITES" ]; then
    CURRENT_SITES="["
  else
    CURRENT_SITES="${CURRENT_SITES::-1},"
  fi
  $OCC config:app:set external sites --value="${CURRENT_SITES}${NEW_SITE}]"
}

do_add_external_site MySite https://thisis.my/site

Thanks for your help!

EWouters avatar Mar 20 '17 04:03 EWouters

Hello.

How to edit icons via "occ"? :)

xaionaro avatar Aug 02 '17 17:08 xaionaro

Got it. It's the third argument, but it's required to save these images to apps/external/img/.

xaionaro avatar Aug 02 '17 17:08 xaionaro

@xaionaro Correct, I did not add an icon in the example, but the bash script also supports adding icons if you place them in ./apps/external/img/ and add the path to the icon as the third argument:

do_add_external_site MySite https://thisis.my/site /var/www/nextcloud/apps/external/img/mysite_icon.png

I am not sure if you need the full path or you can use a path relative to the NextCloud install location, but the full path will definitely work.

EWouters avatar Apr 10 '18 20:04 EWouters

Hi,

can i delete a single site using the occ command?: php occ config:app:delete external sites --value="XXX"

Thanks

julio1501 avatar Jun 06 '19 11:06 julio1501

@julio1501 No, you would delete all of them.

Instead you have to php occ config:app:get external sites and remove the site you want removed from the response, then set the edited response php occ config:app:set external sites --value="EDITED_RESPONSE".

EWouters avatar Jun 07 '19 14:06 EWouters

@EWouters thanks for your reply

julio1501 avatar Jun 12 '19 15:06 julio1501

Fantastic, thank you! I guess this can be closed now, maybe add a link from Readme or info in the docs.

xeruf avatar Nov 15 '22 11:11 xeruf

No, we will keep this open until sites can be managed properly without having to know JSON

nickvergessen avatar Nov 15 '22 11:11 nickvergessen

oh right, I missed that this was still only a script, conflated it with the actual occ command, makes sense :+1:

xeruf avatar Nov 15 '22 23:11 xeruf

Hi ! Thx for the bash script :) Is there any other options that can be added ? Like groups, devices, redirection, ... ?

maxime-collin avatar Feb 20 '23 17:02 maxime-collin

Hi ! Thx for the bash script :) Is there any other options that can be added ? Like groups, devices, redirection, ... ?

Ok found it ! The new conf is now an associative array with all options, perfect ! By the way, I've updated the bash script for the new conf :

#!/bin/bash

usage()
{
echo "Usage:
  ./addExternalSites.sh --name <name> --url <url> [options]

  Add an external site to the current configuration
  url must be a valid URL like (http|https)://(subdomain)?.domain.com/scheme

Options:
  --help                      Display this help message
  --lang <lang>               Can be 'en', 'fr'... default empty (you can easily see available codes by inspect dropdown list on web settings, with a developper tools)
  --icon <icon>               You have to upload the icon via the web interface ! Icon value is the name of the icon like external.svg . Default external.svg
  --type <type>               Where you want the icon shows, value can be : link | settings | quota | guess . Default link
  --device <device>           On which devices this site shows up, value can be : android | ios | desktop | browser . Default empty for all devices
  --groups <group1>,<group2>  Group name must be separate by a semicolon ',' with no blank space. If any group have a blank space in his name, escape the entire block with double quote. Default empty
  --redirect true|false       Default false

Examples:
  ./addExternalSites.sh --name mySite --url http://this.is.my/site --groups admin
"
}

declare -A TYPE_SUPPORTED
for val in link settings quota guest; do
  TYPE_SUPPORTED[$val]="'$val'"
done

declare -A DEVICE_SUPPORTED
for val in android ios desktop browser; do
  DEVICE_SUPPORTED[$val]="'$val'"
done


declare -A REDIRECT_SUPPORTED
for val in true false; do
  REDIRECT_SUPPORTED[$val]="'$val'"
done


while [ $# -gt 0 ]; do
  case $1 in
    --help)
      usage
      exit 0
      ;;

    --name)
      shift
      NAME=$1
      shift
      ;;
    
    --url)
      shift
      URL=$1
      shift
      ;;

    --icon)
      shift
      ICON=$1
      shift
      ;;

    --lang)
      shift
      LANGUAGE=$1
      shift
      ;;

    --type)
      shift
      TYPE=$1
      if ! [[ -n "${TYPE_SUPPORTED[$TYPE]}" ]]; then
        echo "ERROR ! Type found isn't supported, must be ${TYPE_SUPPORTED[*]}"
        exit 2
      fi
      shift
      ;;

    --device)
      shift
      DEVICE=$1
      if ! [[ -n "${DEVICE_SUPPORTED[$DEVICE]}" ]]; then
        echo "ERROR ! Device found isn't supported, must be ${DEVICE_SUPPORTED[*]}"
        exit 3
      fi
      shift
      ;;

    --groups)
      shift
      IFS=',' read -ra GROUP_ARRAY <<< "$1"
      GROUP="["
      for GROUP_NAME in "${GROUP_ARRAY[@]}"; do
        GROUP="${GROUP}\"$GROUP_NAME\","
      done
      GROUP="${GROUP::-1}]"
      shift
      ;;

    --redirect)
      shift
      REDIRECT=$1
      if ! [[ -n "${REDIRECT_SUPPORTED[$REDIRECT]}" ]]; then
        echo "ERROR ! Redirect ound isn't a bool, must be ${REDIRECT_SUPPORTED[*]}"
        exit 4
      fi

      shift
      ;;

    *)
      echo "ERROR ! Unrecognized argument : $1"
      exit 1
      ;;
  
  esac

done


if ! [[ -n $NAME ]]; then
  echo "ERROR ! Name doesn't defined"
  exit 5
fi

if ! [[ -n $URL ]]; then
  echo "ERROR ! Url doesn't defined"
  exit 6
fi

regex="^https?://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]"
if ! [[ $URL =~ $regex ]]; then
  echo "ERROR ! Url isn't one : $URL"
  exit 7
fi


if [ -z $ICON ]; then
  ICON="external.svg"
fi

if [ -z $GROUP ]; then
  GROUP="[]"
fi

if [ -z $REDIRECT ]; then
  REDIRECT="false"
fi

OCC="sudo -u www-data php /var/www/nextcloud/occ"
CURRENT_SITES=$($OCC config:app:get external sites)
ID=$(echo $CURRENT_SITES | awk -F "\"id\":" '{print length($0)-length($NF)}')
if [ $ID -eq 0 ]; then
  ID=1
else
  ID=$(( ${CURRENT_SITES:ID:1} + 1 ))
  CURRENT_SITES="${CURRENT_SITES::-1},}"
fi

NEW_SITE="\"$ID\":{\"id\":$ID,\"name\":\"$NAME\",\"url\":\"$URL\",\"lang\":\"$LANGUAGE\",\"type\":\"$TYPE\",\"device\":\"$DEVICE\",\"icon\":\"$ICON\",\"groups\":$GROUP,\"redirect\":$REDIRECT}"

EXTERNAL_CONFIG="{${CURRENT_SITES:1:-1}${NEW_SITE}}"

$OCC config:app:set external sites --value="${EXTERNAL_CONFIG}"

maxime-collin avatar Feb 20 '23 21:02 maxime-collin

this is somehow broken: occ config:app:set external sites --value={"1":{"id":1,"name":"mySite","url":"http://this.is.my/site","lang":"en_US:en","type":"","device":"","icon":"external.svg","groups":["admin"],"redirect":false}}

output: Config value sites for app external set to {1:redirect:false}

Result:

occ config:app:get external sites
{1:redirect:false}

Versions: nextcloud: 25.0.2.3 apps/external/installed_version: 5.0.2

trefzer avatar May 31 '23 13:05 trefzer

this is somehow broken: occ config:app:set external sites --value={"1":{"id":1,"name":"mySite","url":"http://this.is.my/site","lang":"en_US:en","type":"","device":"","icon":"external.svg","groups":["admin"],"redirect":false}}

output: Config value sites for app external set to {1:redirect:false}

Result:

occ config:app:get external sites
{1:redirect:false}

Versions: nextcloud: 25.0.2.3 apps/external/installed_version: 5.0.2

you need outer quotes: --value="{...}"

rbax avatar Oct 11 '23 19:10 rbax